1

I am new to the java try- catch thing. Can anyone please tell me why this code is an infinite loop? Thanks a lot!

import java.util.*;

public class Exercise12_02 {
  public static void main(String[] args){
    int num1 = 0, num2 = 0;
    Scanner keyboard = new Scanner(System.in);
    boolean status = false;
    while(!status){
    try{
      System.out.print("Enter the first number: ");
      num1 = keyboard.nextInt();
      System.out.print("Enter the second number: ");
      num2 = keyboard.nextInt();
      status = true;
    }catch(InputMismatchException ex){
      System.out.println("Wrong number format.");
    }
  }
    System.out.println("The sum of two numbers are "+ (num1+num2));
  }
}

0 Answers0