Scanner throws InputMismatchException when it is handled

XMLWordPrintable

    • Type: Bug
    • Resolution: Not an Issue
    • Priority: P3
    • None
    • Affects Version/s: 7, 8, 9
    • Component/s: core-libs

      FULL PRODUCT VERSION :


      A DESCRIPTION OF THE PROBLEM :
      In the below code example, the code goes into an infinite loop because the second time sc.nextInt() is called after it throws an exception, it just throws an exception instead of getting input.


      REGRESSION. Last worked in version 6u31

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Run the above code.
      2. Enter a non-number.
      3. See it go into an infinite loop.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      After printing " Not a number " , it should ask the user for another number.
      ACTUAL -
      The program keeps printing " Not a number " .

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      Scanner sc = new Scanner(System.in);
      while(true)
      {
      try
      {
      System.out.print( " Please enter a number " );
      sc.nextInt();
      break;
      }
      catch(InputMismatchException e)
      {
      System.err.println( " Not a number " );
      }
      }
      ---------- END SOURCE ----------

            Assignee:
            Xueming Shen
            Reporter:
            Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: