How and where do i put a try/catch for only entering numbers in a number guessing game?

public class NewClass12 {

    

    public static void main(String[] args) {
        Random generator = new Random();
        int numberToGuess = generator.nextInt(10 - 1) + 1;
        int yourGuess;

        Scanner input = new Scanner(System.in);
        int guess = 0;
        boolean win = false;

        

        while (!win) {
            System.out.println("Guess a number between 1 and 10: ");
            yourGuess = input.nextInt();
            guess++;

            if (yourGuess < 1 || guess > 10) {
                System.out.println("Guess is out of range! Enter a number between 1 and 10");
                continue;
            }

            if (yourGuess == numberToGuess) {
                win = true;
                break;
            }

            if (yourGuess < numberToGuess) {
                System.out.println("Your guess is too low");
            } else {
                
                System.out.println("Your guess is too high");
            }
        }

        if (win) {
            System.out.println("You win!");
            System.out.println("The number was " + numberToGuess);
            System.out.println("It took you " + guess + " tries.");
        
        }
    }
}

so, this is my code for a number guessing game. everything works fine so far execpt if i enter a letter as an input my code crashes. I guess i have to use a try/catch ? If yes where and how do i write it. I am a beginner so have mercy.

Answer

I this case, I would put the try catch wrapping where you read the user’s input, like so:

public class NewClass12 {

    

    public static void main(String[] args) {
        Random generator = new Random();
        int numberToGuess = generator.nextInt(10 - 1) + 1;
        int yourGuess;

        Scanner input = new Scanner(System.in);
        int guess = 0;
        boolean win = false;



        while (!win) {
            System.out.println("Guess a number between 1 and 10: ");
            try{
                yourGuess = input.nextInt();
                guess++;

                if (yourGuess < 1 || guess > 10) {
                    System.out.println("Guess is out of range! Enter a number between 1 and 10");
                    continue;
                }

                if (yourGuess == numberToGuess) {
                    win = true;
                    break;
                }

                if (yourGuess < numberToGuess) {
                    System.out.println("Your guess is too low");
                } else {

                    System.out.println("Your guess is too high");
                }
            } catch (InputMismatchException err){
                System.out.println("The input must be a number!");
                input.next();
            }
        }

        if (win) {
            System.out.println("You win!");
            System.out.println("The number was " + numberToGuess);
            System.out.println("It took you " + guess + " tries.");

        }
    }
}


Note that we need the input.next() within the catch, in order to consume the invalid input, so it won’t be in a loop.

Leave a Reply

Your email address will not be published. Required fields are marked *