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

(scanner) Scanner.next(String pattern) throws InputMismatchException in violation of spec

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • None
    • 7u67
    • core-libs

      A DESCRIPTION OF THE PROBLEM :
      The documentation for the Scanner class claims that the Scanner.next(Pattern) method should only throw one of two exceptions:

      NoSuchElement
      and
      IllegalState

      However, a simple test will show that if the Scanner encounters something that does not match the pattern, it will throw an InputMismatchException.

      Technically, InputMismatchException is a subtype of NoSuchElementException, so the documentation isn't wrong, just misleading. Other methods that throw InputMismatchExceptions explicitly call out this exception (e.g. nextInt).

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Returns the next token if it matches the pattern constructed from the specified string. If the match is successful, the scanner advances past the input that matched the pattern.

      ...


      Throws:
      InputMismatchException - if the next token does not match
      NoSuchElementException - if input is exhausted
      IllegalStateException - if this scanner is closed
      ACTUAL -
      Returns the next token if it matches the pattern constructed from the specified string. If the match is successful, the scanner advances past the input that matched the pattern.

      ...

      Throws:
      NoSuchElementException - if no such tokens are available
      IllegalStateException - if this scanner is closed

      URL OF FAULTY DOCUMENTATION :
      http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#next(java.lang.String)

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

              Created:
              Updated: