-1

I am trying to make a loop that prompts the user for a positive integer so it can be saved into int x.

If a negative or non-integer is entered it will prompt the user again.

It seems to work when a negative value is entered, but not when an exception is entered.

When an exception is entered it just endlessly prints "Please enter a positive integer." without allowing input.

What am I doing wrong? Please help guide me.

Thanks in advance.

int x = 0;

boolean firstLoop = false;
        
while (firstLoop == false)
{
    firstLoop = true;

    try
    {
        x = in.nextInt();
    }
            
    catch (InputMismatchException e)
    {
        System.out.println("Please enter a positive integer.");
        firstLoop = false;
    }
            
    if (x < 0)
    {
        System.out.println("Please enter a positive integer.");
        firstLoop = false;
    }    
}
  • You're probably just getting "InputMismatchException" exception again and again. You need to do something to clear the scanner? Is that what "in" is? – matt Sep 12 '21 at 19:03
  • There's a second bug. If the user types in `-1` he'll get reprompted as intended. If he then types `X` he'll be reprompted twice. The catch-clause should reset x to 0. – user16632363 Sep 12 '21 at 19:20

2 Answers2

0

If you type in an illegal character that can't be parsed to an int, you will get an InputMismatchException. Since firstloop is set to false, the loop continues. But the illegal character is still in the buffer so it continues to repeat. So do something like in.nextLine(); in the first catch block to start over and clear the bad character including the newline.

WJS
  • 36,363
  • 4
  • 24
  • 39
  • Editorial: I've come to the conclusion that Scanner should simply not be taught to beginners; more often than not, they are not equipped with a mental picture of what is going on, particularly with respect to the notional input cursor. This lack shows up in two ways: the confusion about why nextLine() yields an empty string after some nextInt() call or similar, and the present confusion about error recovery. – user16632363 Sep 12 '21 at 19:18
  • @user16632363 correct, Scanner is too circumstantial. Hundreds of questions only on stackoverflow. – Joop Eggen Sep 12 '21 at 19:22
0

You miss in.hasNextInt()/in.hasNextLine()/in.nextLine().

If you can do without console in the IDE, better use java.io.Console instead of Scanner on System.in.

You then need to convert yourself: int Integer.parseInt(String) but it is much cleaner, and allows better prompts.

Console con = System.console();
int x = 0;
for (;;) {
    String input = con.readLine("Please enter a positive integer <= %d: ",
            Integer.MAX_VALUE);
    if (input == null) { // User ended console.
        break;
    }
    if (input.matches("\\d+")) { // 1 or more digits.
        x = Integer.parseInt(input);
        break;
    } else {
        con.printf("This '%s' is not a positive integer.%n", input);
    }
} 
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138