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

Scanner.findInLine does not process empty lines correctly

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 6
    • 5.0
    • core-libs
    • b45
    • x86
    • windows_xp

      FULL PRODUCT VERSION :
      java version "1.5.0_02"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_02-b09)
      Java HotSpot(TM) Server VM (build 1.5.0_02-b09, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      The Scanner does not process empty lines correctly. Specifically, it seems that, in the case of empty input lines (consecutive line-separators), the method findInLine does not preserve the scan state when the specified string or pattern is *not* found. Subsequent lines are then skipped until findInLine succeeds.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile and run the small program attached below.

      It seems significant that this program calls the method findInLine while scanning.



      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      While scanning both strings input1 and input2,
      "line 3" should be printed.
      ACTUAL -
      "line 3" is not printed while scanning input1,
      which is the input with empty lines.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.util.Scanner;

      /**
       * Demonstrate bug in {@link java.util.Scanner}.
       * @author Dave Hale, Colorado School of Mines
       * @version 2005.04.09
       */
      public class ScannerBug {
        private static final String eol = System.getProperty("line.separator");
        private static final String input1 =
          "line 1" + eol +
          "" + eol +
          "line 3" + eol +
          "" + eol +
          "line 5" + eol +
          "" + eol +
          "line 7" + eol;
        private static final String input2 =
          "line 1" + eol +
          " " + eol +
          "line 3" + eol +
          " " + eol +
          "line 5" + eol +
          " " + eol +
          "line 7" + eol;
        private static void scan(String input) {
          Scanner s = new Scanner(input);
          while (s.hasNextLine()) {
            s.findInLine("5");
            System.out.println(s.nextLine());
          }
        }
        public static void main(String[] args) {
          System.out.println("Scanning input with empty lines (incorrectly):");
          scan(input1);
          System.out.println("Scanning input without empty lines (correctly):");
          scan(input2);
        }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Avoid Scanner.findInLine?
      ###@###.### 2005-05-13 11:36:11 GMT

            sherman Xueming Shen
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: