# Counting Occurence of Numbers Entered

• 06-28-2013, 09:47 PM
CarpeDiem
Counting Occurence of Numbers Entered
Hi everyone, In Java book, I have a programming exercises. I am writing exercise in the book:

Write a program that reads the integers between 1 and 100 and counts the occurences of each. Assuma the input ends with 0.
Here is sample run of the proram: 2 5 6 5 4 0
5 occurs 2 times.
2 occurs 1 time.
4 occurs 1 time.
6 occurs 1 time.

My code is below.

import java.util.Scanner;
public class sixpointthree {
public static void main(String [] args){
int a =1;
int numbers[]=new int[100];
int i=-1;
while(a!=0){
Scanner x=new Scanner(System.in);
a=x.nextInt();
i++;
numbers[i]= a;
}
System.out.println(numbers[1]); (Don't care here. I just checked it works)
int counts[]= new int[10];
for(int j=0;j<numbers.length;j++){

(I haven't decided what I do here. I think I should make a new array which contains 10 elements(0,1.....9) but I don't know more. What do you think about this?)

}

}
}
As a result what should I do? Thanks for all response.

Note that: Is topic in wrong place, please replace.
• 06-28-2013, 11:19 PM
CarpeDiem
Re: Counting Occurence of Numbers Entered
That's okey, finally I did this exercises searching other websites and thinking. I am posting all code:

Code:

```import java.util.Scanner; public class sixpointthree {     public static void main(String [] args){     int a =1;     int numbers[]=new int[100];     int i=-1;     while(a!=0){     Scanner x=new Scanner(System.in);     a=x.nextInt();     i++;     numbers[i]= a;     }         int counts[]= new int[100];     int k=-1;     for(int j=0;j<counts.length;j++){                 k++;       counts[j]=find(numbers,k);       }     for(int z=1;z<100;z++){         if(counts[z]!=0&&counts[z]!=1)         System.out.println(z+" occurs "+ counts[z]+" times");         if(counts[z]!=0&&counts[z]==1)             System.out.println(z+ " occurs " + counts[z]+ " time");     }                   }     public static int find(int[] arr, int numTofind){         int occurence=0;         for(int i=0; i<arr.length;i++){             if(arr[i]==numTofind)                 occurence++;         }         return occurence ;     } }```
• 06-28-2013, 11:45 PM
jim829
Re: Counting Occurence of Numbers Entered
Please place your code between code tags [CODE][/CODE]to properly format it. I have done it here.

Code:

```public class Snippet {   public static void main(String[] args) {           int a = 1;           int numbers[] = new int[100];           int i = -1;                   while (a != 0) {                 Scanner x = new Scanner(System.in);                 a = x.nextInt();                 i++;                 numbers[i] = a;           }           // System.out.println(numbers[1]);           // System.out.println(find(numbers,33));           int counts[] = new int[100];           int k = -1;           for (int j = 0; j < counts.length; j++) {                 k++;                 counts[j] = find(numbers, k);           }           for (int z = 1; z < 100; z++) {                 if (counts[z] != 0 && counts[z] != 1)                         System.out.println(z + " occurs " + counts[z] + " times");                 if (counts[z] != 0 && counts[z] == 1)                         System.out.println(z + " occurs " + counts[z] + " time");           }   }   public static int find(int[] arr, int numTofind) {           int occurence = 0;           for (int i = 0; i < arr.length; i++) {                 if (arr[i] == numTofind)                         occurence++;           }           return occurence;   } }```
First, you are going to a lot of unnecessary work to do a frequency count of supplied integers. My first observation is that you are creating some unknown number of scanners in the first while loop. You only need one scanner.

Second, think about what you want to do. Say you read in the number 5. So you now have one 5. Then you read in a 2. Now you have one 2. Then you read in another 5. Now you have two 5's. So you can get the count as you read in the numbers.

Now think about how to store/increment the count for a given number when you read it in. How might you do that? Then when you get 0, you simply print out the various counts.

Regards,
Jim
• 06-29-2013, 02:07 AM
CarpeDiem
Re: Counting Occurence of Numbers Entered
Thanks jim, I changed my post. I already finished this exercises. The above code is true, works correctly.