try/catch with InputMismatchException creates infinite loop

前端 未结 7 890
醉话见心
醉话见心 2020-11-22 13:27

So I\'m building a program which takes ints from user input. I have what seems to be a very straightforward try/catch block which, if the user doesn\'t enter an int, should

相关标签:
7条回答
  • 2020-11-22 13:46

    As the bError = false statement is never reached in the try block, and the statement is struck to the input taken, it keeps printing the error in infinite loop.

    Try using it this way by using hasNextInt()

    catch (Exception e) {
                System.out.println("Error!");
               input.hasNextInt();         
            }
    

    Or try using nextLine() coupled with Integer.parseInt() for taking input....

    Scanner scan = new Scanner(System.in);
    
    int num1 = Integer.parseInt(scan.nextLine());
    int num2 = Integer.parseInt(scan.nextLine());
    
    0 讨论(0)
  • 2020-11-22 13:49

    another option is to define Scanner input = new Scanner(System.in); inside the try block, this will create a new object each time you need to re-enter the values.

    0 讨论(0)
  • 2020-11-22 14:04

    To follow debobroto das's answer you can also put after

    input.reset();
    input.next(); 
    

    I had the same problem and when I tried this. It completely fixed it.

    0 讨论(0)
  • 2020-11-22 14:06

    To complement the AmitD answer:

    Just copy/pasted your program and had this output:

    Error!
    Enter first num: 
    .... infinite times ....
    

    As you can see, the instruction:

    n1 = input.nextInt();
    

    Is continuously throwing the Exception when your double number is entered, and that's because your stream is not cleared. To fix it, follow the AmitD answer.

    0 讨论(0)
  • 2020-11-22 14:07

    YOu can also try the following

       do {
            try {
                System.out.println("Enter first num: ");
                n1 = Integer.parseInt(input.next());
    
                System.out.println("Enter second num: ");
                n2 = Integer.parseInt(input.next());
    
                nQuotient = n1/n2;
    
                bError = false;
            } 
            catch (Exception e) {
                System.out.println("Error!");
                input.reset();
            }
        } while (bError);
    
    0 讨论(0)
  • 2020-11-22 14:08

    @Limp, your answer is right, just use .nextLine() while reading the input. Sample code:

        do {
            try {
                System.out.println("Enter first num: ");
                n1 = Integer.parseInt(input.nextLine());
                System.out.println("Enter second num: ");
                n2 = Integer.parseInt(input.nextLine());
                nQuotient = n1 / n2;
                bError = false;
            } catch (Exception e) {
                System.out.println("Error!");
            }
        } while (bError);
    
        System.out.printf("%d/%d = %d", n1, n2, nQuotient);
    

    Read the description of why this problem was caused in the link below. Look for the answer I posted for the detail in this thread. Java Homework user input issue

    Ok, I will briefly describe it. When you read input using nextInt(), you just read the number part but the ENDLINE character was still on the stream. That was the main cause. Now look at the code above, all I did is read the whole line and parse it , it still throws the exception and work the way you were expecting it to work. Rest of your code works fine.

    0 讨论(0)
提交回复
热议问题