Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8050214

Scanner throws InputMismatchException when it is handled

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P3 P3
    • None
    • 7, 8, 9
    • 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 ----------

            sherman Xueming Shen
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: