0

Why does this code keep on looping if there's an InputMismatchException? When you input a zero, the catch works, but if you enter a string, the loop becomes infinite.

import java.util.*;  
public class TryCatch
{   
    public static void main(String args[])      
    {       
        int age, age2, sum;         
        boolean repeat=true;        

        Scanner input = new Scanner(System.in);                 
        do
        {               
            try
            {
                System.out.println("Enter age: ");              
                age = input.nextInt();              
                System.out.print("Enter age2: ");               
                age2 = input.nextInt();                 
                sum = age / age2;               
                System.out.print(sum);          
                repeat=false;
            }           
            catch (Exception e)
            {           
                System.out.println("Your error is "+e+"\n Try again");                  
            }       
        }       
        while(repeat == true);  
    } 
}
nondestructive
  • 134
  • 2
  • 10
gendave
  • 3
  • 3

2 Answers2

1

Edited after clarifications in comment:

Scanner holds the value that caused the exception and that's why it is triggered again in the following loops.

To obtain what you seek, you need to reset Scanner content and move on to the next input, so change your catch block like this:

        catch (Exception e)
        {           
            System.out.println("Your error is "+e+"\n Try again");
            input.reset();
            input.next();
        }    

OLD:

Because when the Exception happens (not an int in input), your program skips the rest of the code in the try block (and of course the part that stops the loop), and executes the code in the catch block, where it just prints an error message.

In the catch body you need to set repeat=false; in order to have the while instruction to stop repeating.

BeerBaron
  • 388
  • 6
  • 18
  • if i place repeat=false; in the catch body, the loop stops. the problem is, it does not try to accept values again. what i want is that, if there's a type mismatch, it shows the error then prompts for another input until it becomes valid. – gendave Aug 25 '17 at 07:38
  • ahhhh, now it makes sense. I will edit my answer, then.. – BeerBaron Aug 25 '17 at 07:46
  • thanks. it worked. there are other solutions that i found. 1. age = Integer.parseInt(input.nextLine()); 2. Create the input object from Scanner class inside the try body. – gendave Aug 29 '17 at 04:27
0

To correctly catch a bad number, How about

int result = Integer.parseInt(number)

You create an Integer Object and if your input might be 10A, it will throw an NumberFormatException

0x45
  • 779
  • 3
  • 7
  • 26
  • what does "number" represent? I saw another suggestion which solved my problem: age = Integer.parseInt(input.nextLine()); – gendave Aug 25 '17 at 07:33
  • Number is the parsed value from your `Scanner` – 0x45 Aug 25 '17 at 07:35
  • so "number" is "input" in my code? this is the code that i thought solved it: age = Integer.parseInt(input.nextLine()); but there's still an error. Enter age: 6 Enter age2: 2 Your error is java.lang.ArithmeticException: / by zero Try again Enter age: – gendave Aug 25 '17 at 07:42
  • I'd suggest you debug this problem, you will probably find a hint or post the stacktrace... @gendave – 0x45 Aug 25 '17 at 07:45