Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: how to count occurance of numbers using java arrays

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default how to count occurance of numbers using java arrays

    out put comes from this program is:

    1 occurs 1 times
    13 occurs 1 times
    0 occurs 2 times
    9 occurs 0 times
    84 occurs 1 times
    0 occurs 2 times
    13 occurs 3 times
    2 occurs 1 times


    instead of:

    1 occurs 1 times
    13 occurs 3 times
    0 occurs 2 times
    9 occurs 2 times
    84 occurs 1 times
    2 occurs 1 times








    class occu{
    	public static void main(String arg[]){
    	int a[] = {1,13,0,9,9,84,0,13,13,2};
    	int i,j,count=0,temp,pretemp=-99999;
     
    		for(i=0;i<a.length;i++){
    		temp=a[i];
    		if(temp==pretemp)
    			continue;
    		else
    			for(j=0;j<a.length;j++)
    			if(a[j]==temp)
    			count++;
    			System.out.println( a[i]+  " OCCURS " +count+ " TIMES ");
    			count=0;
    			pretemp=a[i];
     
    		} // end for
    	} // end main
    } // end class
    Last edited by pbrockway2; January 1st, 2013 at 04:44 PM. Reason: code tags added


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: how to count occurance of numbers using java arrays

    How does the program accumulate the total counts for each number that is found?

    One problem I see with the code is missing {}s. You should always use {}s with loops and if statements to make sure you know what code goes in the loop or the if statement.

    Also for debugging the Arrays class's toString() method is useful to see what is in an array:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. printing distinct numbers with arrays
    By ColeTrain in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 16th, 2012, 09:32 AM
  2. [METHOD] How: Count how many prime numbers there is between two numbers!
    By Secret20 in forum Object Oriented Programming
    Replies: 4
    Last Post: October 18th, 2011, 02:30 PM
  3. help w/ storing/scanning numbers in arrays
    By robertsbd in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 17th, 2010, 10:55 PM
  4. Conversions of Numbers in Arrays
    By KiwiFlan in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 1st, 2010, 07:59 PM