0

I would like the program to re-do the while loop when it catches the exception - the exception being receiving a text input. Instead it continues a while loop with the code below, I would like it to ask for the user input again.

import java.util.InputMismatchException;
import java.util.Scanner;

public class NumberGuess {
    public static void main(String[] args) {

        Scanner Scanner = new Scanner(System.in);

        int between = 100;
        int secretNumber = (int)(Math.random()*between);
        int inputNum = 0;
        int guesses = 0;

        System.out.println("Please enter your guess: ");
        inputNum = Scanner.nextInt();
        guesses++;

        // ####  Loop here ####
        while (inputNum != secretNumber) {  
            // Try catch
            try {

            // number too high or too low
            if (inputNum > 100 | inputNum < 0) {
                System.out.println("Please enter a guess between 0 and " + between + ".");
                inputNum = Scanner.nextInt();
            }

            // less than secretNumber
            if (inputNum < secretNumber) {
                System.out.println("Try higher");
                inputNum = Scanner.nextInt();
                guesses++;
            }

            // greater than secretNumber
            if (inputNum > secretNumber) {
                System.out.println("Try lower");
                inputNum = Scanner.nextInt();
                guesses++;
            }
            }
            catch(InputMismatchException e){
                System.out.println("Invalid Input");
            }
        }


        System.out.println("\nWell done! The secret number was " + secretNumber + "." + "\nYou took " + guesses +  " guesses.");
        }

}

Output:

Invalid Input
Try higher
Invalid Input
Try higher
Invalid Input
Try higher
Invalid Input
Try higher
Invalid Input
Try higher
Invalid Input
Try higher
Invalid Input
Try higher
Pshemo
  • 122,468
  • 25
  • 185
  • 269

3 Answers3

5

Study the documentation on Scanner:

When a scanner throws an InputMismatchException, the scanner will not pass the token that caused the exception, so that it may be retrieved or skipped via some other method.

If you catch the exception, the Scanner is left where it was before trying to read the number. You must advance it with nextLine within the catch-block.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • Brilliant - that worked. Simply added "Scanner.nextLine();" to the catch method. Thanks alot. –  Nov 17 '13 at 15:24
0

| is logical operator. Use conditional ||

 if (inputNum > 100 || inputNum < 0) {
Masudul
  • 21,823
  • 5
  • 43
  • 58
0

Make 2 changes as following can help you implement the function you want.

1 Change if (inputNum > 100 | inputNum < 0) { to if (inputNum > 100 || inputNum < 0) {

If you enter non-digital numbers, such as a, the InputMismatchException Exceptiom can be caugt.

  1. When InputMismatchException Exception happen, add code call Scanner.next() to make it possible to re-enter number in Console.

Change

        // greater than secretNumber
        if (inputNum > secretNumber) {
            System.out.println("Try lower");
            inputNum = Scanner.nextInt();
            guesses++;
        }
        }
        catch(InputMismatchException e){
            System.out.println("Invalid Input");

        }

To

        // greater than secretNumber
        if (inputNum > secretNumber) {
            System.out.println("Try lower");
            inputNum = Scanner.nextInt();
            guesses++;
        }
        }
        catch(InputMismatchException e){
            System.out.println("Invalid Input");
            Scanner.next();

        }

With the above changes, a test is as follows:

Please enter your guess: 
60
Try higher
80
Try higher
90
Try lower
a
Invalid Input
Try lower
80
Try higher
85
Try lower
84
Try lower
83
Try lower
82
Try lower
81

Well done! The secret number was 81.
You took 9 guesses.
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Mengjun
  • 3,159
  • 1
  • 15
  • 21