Almac
Almac

Reputation: 227

How to return the Max Value and Max Count from a user input

I'm trying to get both, the largest number and the largest occurring number, from a user input. The problem with my code is it only returns the first value of the array.

public class CountMax {
public static void main(String [] args) {
    //Create scanner object
    Scanner input = new Scanner(System.in);

    //Obtain user input 
    System.out.println("Enter numbers: ");
    int num = input.nextInt(); 
    int array[] = new int[num]; 

    //loop through array
    int max = array[0];
    int count = 1;
        for (int i = 0; i < array.length; i++) { 
            array[i] = num; 
            if(array[i] > max) {
                max = array[i];
                count = 1;
            } else if(array[i] == max) {
                count++;
            }
        }
    //output results
    System.out.println("The largest number is " + max);
    System.out.println("The occurrence count of the largest number is " + count);
}}

Upvotes: 1

Views: 7562

Answers (3)

Oskar
Oskar

Reputation: 15

I know this post is old, but Wyatt Lowery's solution is incorrect, just in case someones stumbles upon it from Google just like I did. You cannot count the number of max values in an array in the same loop like that until you have found the max value.

Example using Wyatt's class: 2 is obviously an incorrect answer.

Enter numbers: 
1, 2, 3, 4, 5, 5, 7
The largest number is 7
The occurrence count of the largest number is 2

I would do:

int max = array[0];
int sum = 0;
for(int i = 1; i < array.length; i++) {
    if(array[i] > max) max = array[i];
}
for(int i = 0; i < array.length; i++) {
    if(array[i]==max) sum++;
}

Upvotes: 2

Wyatt Lowery
Wyatt Lowery

Reputation: 513

One problem I noticed:

int num = input.nextInt();

When you do this, it is only going to take the first int (Meaning, only 1 number) As well when you are creating your array int array[] = new int[num], you are creating an array with the SIZE of num, and not actually creating an array with the VALUES of num. (Even though num is only a single number) To actually create an array of numbers, do something like this:

System.out.pritnln("Enter in numbers:");
String[] array = input.nextLine().split(", ");

An example input would be: "13, 12, 14, 14". Then the contents of the array would be those terms (And would remove spaces & commas). Your program should look something like this when finished:

public class CountMax {
public static void main(String [] args) {
    //Create scanner object
    Scanner input = new Scanner(System.in);

    //Obtain user input
    System.out.println("Enter numbers: ");
    String[] array = input.nextLine().split(", ");

    //Loop through array
    int max = Integer.parseInt(array[0]);
    int count = 0;
    for (int i = 0; i < array.length; i++) {
        if(Integer.parseInt(array[i]) > max) {
            max = Integer.parseInt(array[i]);
        } else if(Integer.parseInt(array[i]) == max) {
            count++;
        }
    }
    //Output 
    System.out.println("The largest number is " + max);
    System.out.println("The occurrence count of the largest number is " + count);
    }
}

Hope this helped :-)

Upvotes: 1

germas
germas

Reputation: 1

Think more carefully about each step you need to take.

Do you know how many numbers will be entered by the user? Right now you are only taking in one number because you are not looping on the input

int num = input.nextInt(); 
int array[] = new int[num];

Here, you are creating an array the size of whatever number the user entered. This is a correct approach, more typical of C, if the user will tell you "I will enter 10 numbers" and then enters the 10 numbers. This is convenient because you will know to loop 10 times, and you will need to count a maximum of 10 different numbers.

If we don't know how many numbers will be entered you will need to loop until EOF.. something like

while(input.hasNext()) {
   int currentInt = input.next();
   ...
}

Now you have to consider how you will be counting these items.

I hope this gives you some things to think about towards your solution..

Upvotes: -1

Related Questions