Results 1 to 4 of 4
  1. #1
    CarpeDiem is offline Member
    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0

    Default 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.
    Last edited by CarpeDiem; 06-28-2013 at 09:52 PM.

  2. #2
    CarpeDiem is offline Member
    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0

    Default 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[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 ;
        }
    }
    Last edited by CarpeDiem; 06-29-2013 at 02:05 AM.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,318
    Rep Power
    5

    Default 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[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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    CarpeDiem is offline Member
    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0

    Default Re: Counting Occurence of Numbers Entered

    Thanks jim, I changed my post. I already finished this exercises. The above code is true, works correctly.

Similar Threads

  1. Replies: 12
    Last Post: 09-08-2011, 05:03 PM
  2. Counting occurence of a word in file
    By KAS in forum New To Java
    Replies: 15
    Last Post: 05-16-2011, 06:47 PM
  3. Counting numbers up and down
    By radio in forum New To Java
    Replies: 4
    Last Post: 05-06-2011, 03:03 PM
  4. Searching the first occurence
    By The Hawk in forum New To Java
    Replies: 7
    Last Post: 11-29-2009, 12:36 PM
  5. Can somebody check my codes?(Counting numbers)
    By PureAwesomeness in forum New To Java
    Replies: 8
    Last Post: 07-01-2009, 03:03 PM

Posting Permissions

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