Thread: Counting Occurence of Numbers Entered

1. Member Join Date
Jun 2013
Posts
3
Rep Power
0 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;
int i=-1;
while(a!=0){
Scanner x=new Scanner(System.in);
a=x.nextInt();
i++;
numbers[i]= a;
}
System.out.println(numbers); (Don't care here. I just checked it works)
int counts[]= new int;
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.
Last edited by CarpeDiem; 06-28-2013 at 09:52 PM.  Reply With Quote

2. Member Join Date
Jun 2013
Posts
3
Rep Power
0 Re: Counting Occurence of Numbers Entered

That's okey, finally I did this exercises searching other websites and thinking. I am posting all code:

Java Code:
import java.util.Scanner;

public class sixpointthree {
public static void main(String [] args){
int a =1;
int numbers[]=new int;
int i=-1;
while(a!=0){
Scanner x=new Scanner(System.in);
a=x.nextInt();
i++;
numbers[i]= a;
}

int counts[]= new int;
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 ;
}
}
Last edited by CarpeDiem; 06-29-2013 at 02:05 AM.  Reply With Quote

3. Senior Member Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
13 Re: Counting Occurence of Numbers Entered

Please place your code between code tags [CODE][/CODE]to properly format it. I have done it here.

Java Code:
public class Snippet {
public static void main(String[] args) {
int a = 1;
int numbers[] = new int;
int i = -1;

while (a != 0) {
Scanner x = new Scanner(System.in);
a = x.nextInt();
i++;
numbers[i] = a;
}
// System.out.println(numbers);

// System.out.println(find(numbers,33));

int counts[] = new int;
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  Reply With Quote

4. Member Join Date
Jun 2013
Posts
3
Rep Power
0 Re: Counting Occurence of Numbers Entered

Thanks jim, I changed my post. I already finished this exercises. The above code is true, works correctly.  Reply With Quote Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•