# the count on my program is not working as i would like it to

• 02-26-2013, 11:56 PM
abi
the count on my program is not working as i would like it to
The program is meant to take in user entered numbers (until the user enters 0) and evaluate which is the highest of the numbers that were entered and show the occurrence of the no.

eg:
Enter numbers: 1 2 3 7 7 6 5 5 0
The largest number is 7
Its occurrence count is 2

I cant figure out where to put my occurrence count and get the accurate result for it.

Code:

import java.util.*;

public class Max
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);

System.out.print("Enter numbers: ");
int a = input.nextInt();
int count = 0;

int large = -1;

do
{
a = input.nextInt();

if ( large < a )
{
large = a;

}

if( a == large )
{
count++;
}

} while ( a != 0);

System.out.println("The largest number is: " + large);
System.out.print("The occurance count of the largest number is: " + count);

}
}

• 02-27-2013, 12:44 AM
jim829
Re: the count on my program is not working as i would like it to
You're almost there. You need to reset count each time you assign "a"to "large."

Jim
• 02-27-2013, 01:13 AM
Wizard0860
Re: the count on my program is not working as i would like it to
Try using math.max(int1,int2) if the max doesn't work for you. Its just easier, you could make an array, and have it holt the numbers and be one of the ints. As for occurrence, the result could be an int, and have a variable set to 0 (lets call it stool), and then after all the inputs are entered, have it like read them again or something, and idk if this works, you could try if(input.nextInt==whatever variable you set the result of math.max as) stool++; then return stool before end of program, and have the occurrences be "..."+stool. Idk, im pretty new at java, idk if this will work.
• 02-28-2013, 01:49 AM
abi
Re: the count on my program is not working as i would like it to
I realized that after i posted this. Thanks.
• 02-28-2013, 01:50 AM
abi
Re: the count on my program is not working as i would like it to
I am a beginner, haven't learned about arrays yet. I got it to work though. Thanks.
• 02-28-2013, 03:46 AM
Wizard0860
Re: the count on my program is not working as i would like it to
Yeah I just recently learned about them too. Only been learning for 6 months now.