The Artima Developer Community
Sponsored Link

Java Answers Forum
why wont this work

3 replies on 1 page. Most recent reply: Nov 15, 2003 4:10 PM by Jonathon Brozny

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
sean fitzgerald

Posts: 2
Nickname: fritz
Registered: Nov, 2003

why wont this work Posted: Nov 14, 2003 6:07 PM
Reply to this message Reply
Advertisement
im making a program that accepts ten numbers the user inputs between 1 and 50 and outputs how many times each number was used. i can count everything, the only trouble i am having is condensing how many times it says how many times each number appears. my technique is having three arrays, one to keep track of what the user inputs, two to keep track of how many times each number is inputted, and three to condense it. the trouble i am having is in between lines 37 and 43 (if you copy+paste). here i am going through each number in the third array to see if the number after it is equal to it, and if it is, then change the number into -1. for some reason, it changes every number into -1 except the first one and i can't figure out what i am doing wrong. it has been taking me nearly a day to figure out what is wrong with this piece of code (37-43). Please help me! thanks!

import java.io.*;

public class Assignfour {
public static void main(String[] args) throws IOException {
BufferedReader userin = new BufferedReader (new InputStreamReader (System.in));
//declarations
int x = 0, y = 0, z = 0;
int[] arrone = new int[10];
int[] arrtwo = new int[10];
int[] arrthree = new int[10];
//introduction
System.out.println ("***********************");
System.out.println ("* Assignment Four *");
System.out.println ("***********************\n");
System.out.println ("Input ten numbers between 1 and 50. The object");
System.out.println ("of the program is to identify how many times");
System.out.println ("you use each number, so it is okay to use numbers over again\n");
//user enters numbers, computer analyzes number to see if it is valid
while (x != 10) {
System.out.print ("Enter #" + (x + 1) + ": ");
arrone[x] = Integer.parseInt(userin.readLine());
while (arrone[x] < 0 || arrone[x] > 50) {
System.out.println ("\nThat number is not between 0 and 50.");
System.out.print ("Enter #" + (x + 1) + ": ");
arrone[x] = Integer.parseInt(userin.readLine());
}//while
x++;
}
System.arraycopy(arrone, 0, arrthree, 0, 10);
for (x = 0; x < 10; x++) {
for (y = 0; y < 10; y++) {
if (arrone[x] == arrone[y]) {
arrtwo[x]++;
}
}
}
for (x = 0; x < 10; x++) {
for (y = 1; y < 10; y++) {
if (arrthree[x] == arrthree[y]) {
arrthree[y] = -1;
}
}
}
for (x = 0; x < 10; x++) {
System.out.println ("arrone: " + arrone[x] + ". arrtwo: " + arrtwo[x] + ". arrthree: " + arrthree[x] + ".");
}
for (x = 0; x < 10; x++) {
if (arrthree[x] > -1) {
System.out.println ("The number " + arrone[x] + " has been used " + arrtwo[x] + " times.");
}
}
}//main
}//class


Jonathon Brozny

Posts: 24
Nickname: jonathon
Registered: Oct, 2003

Re: why wont this work Posted: Nov 14, 2003 10:53 PM
Reply to this message Reply
How does this work, I just modified the one loop, and changed the condition at the bottom to 0 instead of -1, shouldn't need the arrthree anymore.
import java.io.*;
public class Assignfour
{
	public static void main(String[] args) throws IOException
	{
		BufferedReader userin = new BufferedReader(new InputStreamReader(System.in));
		//declarations
		int x = 0, y = 0, z = 0;
		int[] arrone = new int[10];
		int[] arrtwo = new int[10];
		int[] arrthree = new int[10];
		//introduction
		System.out.println("***********************");
		System.out.println("*    Assignment Four  *");
		System.out.println("***********************\n");
		System.out.println("Input ten numbers between 1 and 50. The object");
		System.out.println("of the program is to identify how many times");
		System.out.println("you use each number, so it is okay to use numbers over again\n");
		//user enters numbers, computer analyzes number	to see if it is valid
		while (x != 10)
		{
			System.out.print("Enter #" + (x + 1) + ":  ");
			arrone[x] = Integer.parseInt(userin.readLine());
			while (arrone[x] < 0 || arrone[x] > 50)
			{
				System.out.println("\nThat number is not between 0 and 50.");
				System.out.print("Enter #" + (x + 1) + ":  ");
				arrone[x] = Integer.parseInt(userin.readLine());
			} //while
			x++;
		}
		System.arraycopy(arrone, 0, arrthree, 0, 10);
		for (x = 0; x < 10; x++)
		{
			boolean incremented = false;
			for (y = 0; y < 10; y++)
			{
				if (arrone[x] == arrone[y] && !incremented)
				{
					incremented = true;
					arrtwo[y]++;
				}
			}
		}
		for (x = 0; x < 10; x++)
		{
			System.out.println("arrone: " + arrone[x] + ". arrtwo: " + arrtwo[x] + ". arrthree: " + arrthree[x] + ".");
		}
		for (x = 0; x < 10; x++)
		{
			if (arrtwo[x] > 0)
			{
				System.out.println("The number " + arrone[x] + " has been used " + arrtwo[x] + " times.");
			}
		}
	} //main
} //class

sean fitzgerald

Posts: 2
Nickname: fritz
Registered: Nov, 2003

Re: why wont this work Posted: Nov 15, 2003 11:14 AM
Reply to this message Reply
well it is still not doing what im having trouble with though, which is the part when it displays:

"the number _ has been used _ times"

it should only display the amount of different numbers used, so it doesnt say:

"the number 5 has been used 4 times"
"the number 5 has been used 4 times"

and it should only display each number once instead of more than once... the only real trouble i had was with the 2nd outer FOR loop where it changes each number in arrthree to -1. it was changing every number to -1 (except the first) instead of just the duplicates, which was the prime error of the program.

Jonathon Brozny

Posts: 24
Nickname: jonathon
Registered: Oct, 2003

Re: why wont this work Posted: Nov 15, 2003 4:10 PM
Reply to this message Reply
Did you run it? this is the output that i just got from the code I modified.

Enter #1: 1
Enter #2: 1
Enter #3: 2
Enter #4: 7
Enter #5: 8
Enter #6: 9
Enter #7: 9
Enter #8: 11
Enter #9: 13
Enter #10: 11
arrone: 1. arrtwo: 2. arrthree: 1.
arrone: 1. arrtwo: 0. arrthree: 1.
arrone: 2. arrtwo: 1. arrthree: 2.
arrone: 7. arrtwo: 1. arrthree: 7.
arrone: 8. arrtwo: 1. arrthree: 8.
arrone: 9. arrtwo: 2. arrthree: 9.
arrone: 9. arrtwo: 0. arrthree: 9.
arrone: 11. arrtwo: 2. arrthree: 11.
arrone: 13. arrtwo: 1. arrthree: 13.
arrone: 11. arrtwo: 0. arrthree: 11.
The number 1 has been used 2 times.
The number 2 has been used 1 times.
The number 7 has been used 1 times.
The number 8 has been used 1 times.
The number 9 has been used 2 times.
The number 11 has been used 2 times.
The number 13 has been used 1 times.

Flat View: This topic has 3 replies on 1 page
Topic: Need help with these questions.... Previous Topic   Next Topic Topic: how to switch from http to https ?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use