Home > other >  Scanner is trying to read more numbers than expected and skips some other numbers
Scanner is trying to read more numbers than expected and skips some other numbers

Time:09-17

import java.util.Scanner;

public class Main
{
    public static void main(String[] args) {
    
    Scanner scanner = new Scanner (System.in);
    
    System.out.println("Enter numbers, input ends with 0: ");
    int max = 0;
    int count = 0;
    while (scanner.nextInt() != 0 )
    {
        if (scanner.nextInt() > max)
        {
            max = scanner.nextInt();
            count = 1;
        }
        else if (scanner.nextInt() == max)
        {
            count  ;
        }
    
        }
    if (max == 0 && count == 0)
    {
     System.out.println("No numbers are entered except zero.");

    }
    else
    {
        System.out.println("The largest number is "   max);
        System.out.println("The occurence count of the largest number is "   count);
    }

    }
        
}

The above code is what I have managed to create thus far in an attempt to create a program that will identify the largest number, and also take a count of how many times that number is listed. I would also like the number 0 to trigger the end of the input sequence.

For example, if I type:

2 3 9 4 5 9 3 3 0

I should receive a max of 9 and a count of 2.

The code seems to work sometimes, and other times does not return any output when a sequence ending in 0 is entered. I also get an incorrect count but correct max other times.

Please help me understand what is going haywire with my code.

CodePudding user response:

Every time you invoke scanner.nextInt(), it's (waiting for and) reading another int from input.

The times when it doesn't seem to complete is when you enter a sequence without a multiple of 3 entries, e.g. 1 0: the while condition reads the 1; the if condition reads the 0 (which doesn't cause the loop to break); and the max = waits for you to enter another number.

Don't keep invoking it: assign int next = scanner.nextInt(), and use that value:

while (true) {
  int next = scanner.nextInt();

  if (next == 0) break;

  if (next > max) { ... }
  else if (next == max) { ... }
}

CodePudding user response:

#Mind you that as far as your while loop is concerned, no command in the while loop will be executed whenever you enter zero[0] as an input because what you instructed the while loop to do is to run when the input is not zero so no need putting a condition to check whether an input is zero or not.

Then just as @Andy Turner said with small editon to his code do this to get things done:


   //use the value "true" rather than any other condition to keep the loop in iteration.
//The boolean value "true" means continue  reading and accepting input forever unless there is a condition that will break out of it inside the while loop
while (true) {
  int next = scanner.nextInt();

  if (next == 0){
//Do your prefered stuff here when the input evaluates to zero and then break out of the loop.
 break;
}

  if (next > max) { ... }
  else if (next == max) { ... }
}
  • Related