Ok, in my Java programming class we have to write a program that reads some integers between 1 and 100 and counts the occurrences of each. Assume the input ends with 0. Here is a sample run of the program:
Enter some integers between 1 and 100: 2 5 6 5 4 3 23 43 2 0
2 occurs 2 times
3 occurs 1 time
4 occurs 1 time
5 occurs 2 times
23 occurs 1 time
43 occurs 1 time
Note that if a number occurs more than one time, the plural word "times" is used in the output. Also,
note that the integers input will be between 1 and 100 but there may be more than 100 numbers input.
Finally, note that for integers input 0 times, nothing is printed.
Ok so here's what I have so far, but the program won't run! If you could be as thorough as possible, I'm a beginner and terrible at programming! Thanks so much!
import java.util.Scanner; class NumberOccurance{ public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the integers between 1 and 100: "); int i, count[] = new int[101]; while (input.hasNextInt()) count[input.nextInt()]++; for (i=1; i < 101; i++) if (count[i] != 0) System.out.println(i+" occurs "+count[i]+" time"+(count[i]>1?"s":"")); } }
When I debug this in netbeans it allows me to enter the numbers but I get no response what is wrong? What am I missing?