0

I am new to java and am trying to add error handling to my projects. In this number generator, I want to add a try catch function to catch exceptions from not entering a number. I have read other people's try catch options and pasted my code in their loops, however my code either:

1) Is stuck repeating code in catch block

2) Performs code in catch block and ends without repeating loop

I have tried countless edits to fix this but I keep getting one of those two results :( Thank you for any help!

(This code repeats catch block print message)

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

public class GuessTheNumber {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Random rand = new Random();
        int number = rand.nextInt(21);
        int guess = -1;
        System.out.print("Guess a number between 0 and 20: ");

        do {
            try {
                guess = input.nextInt();
                if (guess > number)
                    System.out.print("Number is too high! ");
                else if (guess < number)
                    System.out.print("Number is too low! ");
                else {
                    System.out.print("That's correct! ");
                    System.out.printf("My number was %d!", number);
                }
            } catch (InputMismatchException e) {
                System.out.println("That's not a number!");
            }
        } while (guess != number);
    }
}
TreffnonX
  • 2,924
  • 15
  • 23
jovartzy
  • 9
  • 1
  • and what is the actual problem? have you debugged your code? do you always get an InputMismatch? – Stultuske Jun 08 '20 at 06:58
  • Try using nextLine instead of nextInt and Integer.parseInt, because you might be missing the \n and then the input is mismatched. – Fernando Martín Besteiro Jun 08 '20 at 06:59
  • @Stultuske My issue is I want the code to continue rather than get stuck in the catch loop or throw an exception then end. Also every time I put a value in the input that is not a number it throws an InputMismatchException. Also just using general "Exception" gives me the same results. – jovartzy Jun 08 '20 at 07:08
  • @jovartzy yes, it keeps continuing. the actual problem is why it keeps continuing. Have you debugged your code to check the values you have compared? have you tried adding some print statements? Why would using the more general Exception type change the behavior? Have you tried Fernando's suggestion? – Stultuske Jun 08 '20 at 07:11
  • You need to `break` out of your loop in the `else` statement when the number has been guessed correctly – Lino Jun 08 '20 at 07:15
  • @FernandoMartínBesteiro Thank you! I switched to nextInt and Intege.parseInt() and now it works and I had to change the exception to NumberFormatException. I am still not exactly sure why the other way was not working, I did not change to nextLine(), I simply changed from nextInt() to Integer.parseInt(), I will have to look into the InputMismatchException! Thanks again! – jovartzy Jun 08 '20 at 07:15
  • @jovartzy "I simply changed from nextInt() to Integer.parseInt() " ... ehm ... so, how do you get your input to parse, if you don't use nextLine() ? – Stultuske Jun 08 '20 at 07:17
  • @rghome yes that is exactly what I needed! I appreciate it! – jovartzy Jun 08 '20 at 07:17
  • @Stultuske my bad, I changed it to this format: guess = Integer.parseInt(input.next()); – jovartzy Jun 08 '20 at 07:19

0 Answers0