1

I coded a program, that calculates the gcd (greatest common divisor) and lcm (least common multiple). Everything works fine except the try {...} catch(...) {...}. Here is the part of the code that doesn't work as I want it to:

try {
    num1 = Integer.parseInt(sc.nextLine());
}
catch(Exception e) {
    System.out.println("Your input is not an integer (number w/o decimals)! Try again.");
    System.out.print("Enter your first number: ");
    num1 = Integer.parseInt(sc.nextLine());
}

When I input e.g. letters, it says:

Your input is not an integer (number w/o decimals)! Try again.
Enter your first number:

But when I type letters the second time, the program crashes:

Exception in thread "main" java.lang.NumberFormatException: For input string: "asdf"
    at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68)
    at java.base/java.lang.Integer.parseInt(Integer.java:658)
    at java.base/java.lang.Integer.parseInt(Integer.java:776)
    at GCDLCMgetter.main(GCDLCMgetter.java:56)

It is probably a very simple mistake I made but I can't figure it out...

Thank you

Christian S.
  • 877
  • 2
  • 9
  • 20
Dudek
  • 54
  • 1
  • 7
  • 3
    `num1 = Integer.parseInt(sc.nextLine());` should not be placed within `catch`; I imagine you want to execute this in a loop. – Jacob G. Jan 24 '20 at 20:03

4 Answers4

2

Your second parseInt method call is not in try catch block. You need to use a loop for this kind of logic.

H.Ç.T
  • 3,335
  • 1
  • 18
  • 37
1

When the first time you give letters it goes into catch block. Displays the error message. And then executes the line num1 = Integer.parseInt(sc.nextLine()); Again you have entered the letters, but this time there is no try-catch block to handle this. So it throws error.

Rahul Agrawal
  • 623
  • 1
  • 5
  • 18
1

It's because your second prompt is inside the catch block. Instead of prompting again inside the catch block, you want to wrap the entire code section in a loop so it comes back around to the try block for the prompt again. Something like:

boolean repeat = true;
while(repeat){
    try{
        //Prompt for input
        repeat = false;
    }catch(Exception e) {
        //Display error message
    }
}
Tim Hunter
  • 826
  • 5
  • 10
1

In your code, it gets executed twice:

  1. Reads a Line at try{...}
  2. There is an Exception
  3. The Exception is handled by the catch(Exception e){...}
  4. The num1 = Integer.parseInt(sc.nextLine()); inside the catch(Exception e){...} cannot be handled. It's not placed inside try{}.
  5. Execution finished because the last exception cannot be handled by any catch.

It seems you're using Scanner, I would recommend you using a loop whis way:

while (sc.hasNextLine()){
    try {
        System.out.print("Enter your first number: ");
        num1 = Integer.parseInt(sc.nextLine());
    }
    catch(Exception e) {
        System.out.println("Your input is not an integer (number w/o decimals)! Try again.");
    }
}

If you're managing integers, it would be interesting using Scanner.nextInt()

while (sc.hasNextInt()){
    try {
        System.out.print("Enter your first number: ");
        num1 = sc.nextInt());
    }
    catch(Exception e) {
        System.out.println("Your input is not an integer (number w/o decimals)! Try again.");
    }
}
tremendows
  • 4,262
  • 3
  • 34
  • 51