简体   繁体   中英

Problems with try-catch inside while loop in Java

I'm new here and I'm learning Java. In one of my programs, I have made a guessing game. The guessing game is supposed to keep asking the user to input a guess until they guess the number right.

This is my code:

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

public class Main {
    public static void main(String[] args) {
        final int minValue = 1;
        final int maxValue = 10;
        final boolean displayHints = true;  // Display whether the number is too high or too low when guessed incorrectly?
        int tries = 1;
        int guess = 0;      // We need to give 'guess' a (temporary) value or else the 'while' loop will create an error
        boolean error = false;

        Random generator = new Random();                        // Create scanner 'generator'
        int random = generator.nextInt(maxValue) + minValue;    // Define 'random' variable with a random value
        if (random == guess) {  // In case 'random' = 'guess'
            guess = -852654;
        }
        Scanner input = new Scanner(System.in); // Create a scanner
        System.out.println("Random number: " + random); // Hey, no cheating! (for debugging purposes)

        System.out.println("Try to guess the magic number! (from " + minValue + " to " + maxValue + ")");
        while (random != guess) {
            do {    // Supposed to ask the user to input a number until they enter a valid number. This is the part of the code that is not working.
                System.out.println("\nInput your guess now!");
                try {
                    guess = input.nextInt();
                    error = false;
                } catch (InputMismatchException e) {
                    System.err.println("That's not a number!\n");
                    error = true;
                    continue;
                }
            } while (error);

            if (guess == random) {
                System.out.println("Correct!");
                System.out.println("Number of tries: " + tries + ".");
                input.close();
            } else {
                tries++;
                if (displayHints) {
                    if (guess < random) {
                        System.out.println("Sorry, too low!");
                    } else if (guess > random) {    // not strictly necessary
                        System.out.println("Sorry, too high!");
                    }
                } else {
                    System.out.println("Sorry, that was not the right number");
                }
            }
        }
    }
}

The code is pretty self-explanatory, because I made a lot of comments. The problem, though, is when the user enters an invalid integer (like 'banana'), instead of saying "That's not a number!" and asking for another number, the code does something like this:

Random number: 9
Try to guess the magic number! (from 1 to 10)

Input your guess now!
banana

Input your guess now!

Input your guess now!

Input your guess now!

Input your guess now!

Input your guess now!

Input your guess now!

Input your guess now!

Input your guess now!

Input your guess now!

Input your guess now!
That's not a number!

Input your guess now!

That's not a number!

That's not a number!

That's not a number!

That's not a number!

That's not a number!

That's not a number!

That's not a number!

That's not a number!

That's not a number!

That's not a number!

That's not a number!


Input your guess now!
That's not a number!


Input your guess now!
That's not a number!


Input your guess now!
That's not a number!


Input your guess now!
That's not a number!


Input your guess now!
That's not a number!


Input your guess now!
That's not a number!

The rest of the code works perfectly.

You forgot to consume the bad input. Try consuming the line with the bad input in the catch block.

} catch (InputMismatchException e) {
    System.err.println("That's not a number!\n");
    error = true;
    String notANumber = input.nextLine();  // add
    continue;
}

Also, println already adds a newline character at the end of whatever is printing, so there is no need to add additional \\n characters to the strings you're printing.

With the above change, here's sample input/output of the do-while loop:

Input your guess now!
banana
That's not a number!


Input your guess now!
8

As explained by rgettman you need to consume the erroneous input, since if InputMismatchException is risen, the token is not consumed.

An alternative solution, to save you from the try/catch block would be to use hasNextInt() :

if (input.hasNextInt())
{
  int guess = input.readInt();
}
else
{
  if (input.hasNextLine())
    input.nextLine();
}

The scanner never actually gets a valid input, so it is repeatedly grabbing banana over and over when you reach guess = input.nextInt();

My fix would be to instead read in the input as a string and parse it to an integer. You would then just need to catch a NumberFormatException instead of a InputMismatchException

This is how I would do it:

try {
    guess = Integer.parseInt(input.next());
    error = false;
} catch (NumberFormatException e) {
    System.err.println("That's not a number!\n");
    error = true;
}

As many have mentioned, you need to consume the erroneous input. I was having a very similar problem, and I didn't see an appropriate response here, but I found one elsewhere.

Try putting the following line at the end of your catch block.

input.nextLine();

This will clear the buffer and should fix your problem.

The easiest way is just to change

guess = input.nextInt();

to

guess = Integer.valueOf(input.next());

That will solve the problem, only with changing one small line of code. Copy and try it!

But I still think your code looks messy. I would do something like this

  public static void main(String[] args) {



    Scanner input = new Scanner(System.in);
    Random r = new Random ();
    int x = r.nextInt(10);
    int y = 0;
    int counter=0;


    do{
    System.out.println("Guess a number between 0-10: ");

    try{
    y = Integer.valueOf(input.next());
    }catch (Exception e){
        System.out.println("That is not a number ");
        continue;
    }
    counter ++;

    if (counter>5){
    System.out.println("So you still don't know how to guess quicker?");
    }

    if (y<x){
    System.out.println("You gessed wrong, the number is higher");
    }

    else if (y>x){
    System.out.println("You gessed wrong, the number is lower");
    }

    else if (y==x)
        System.out.println("You gessed right, the number is: " + x);

        }while(y!=x);

    System.out.println("You guessed the number in: " + counter + " times");
    if(counter <=4){
 System.out.println("You found out how to guess the number quickly");
    }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM