0
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.

markspace
  • 9,246
  • 2
  • 20
  • 35
  • Well the first thing you should do is look at the stack trace when it crashes and see the line number that it tells you the problem is. That should be your first choice when thinking about where to add things to code, like try/catch. – markspace Mar 31 '21 at 22:10
  • Does this answer your question? [Validating input using java.util.Scanner](https://stackoverflow.com/questions/3059333/validating-input-using-java-util-scanner) – Tom Mar 31 '21 at 22:11
  • @markspace Exception in thread "main" java.util.InputMismatchException at java.base/java.util.Scanner.throwFor(Scanner.java:939) at java.base/java.util.Scanner.next(Scanner.java:1594) at java.base/java.util.Scanner.nextInt(Scanner.java:2258) at java.base/java.util.Scanner.nextInt(Scanner.java:2212) at NewClass12.main(NewClass12.java:31 – JustAnAmateur Mar 31 '21 at 22:15
  • yourGuess = input.nextInt(); is the line where the problem is – JustAnAmateur Mar 31 '21 at 22:16
  • @Tom i think example 1 is what i need, but where do i put it in my code? right after the while loop ? – JustAnAmateur Mar 31 '21 at 22:17
  • 1
    Please don't add comments to clarify, [edit] your question instead. Code is unreadable in comments, and they don't appear in chronological order but by number of votes. – Robert Mar 31 '21 at 22:41
  • @Robert i will in the next time. I am new to stack overflow so have mercy :) – JustAnAmateur Mar 31 '21 at 22:45

1 Answers1

0

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.