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
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.*;
publicclass Assignfour
{
publicstaticvoid main(String[] args) throws IOException
{
BufferedReader userin = new BufferedReader(new InputStreamReader(System.in));
//declarations
int x = 0, y = 0, z = 0;
int[] arrone = newint[10];
int[] arrtwo = newint[10];
int[] arrthree = newint[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
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.
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.