JDK-6269946 : Scanner.findInLine does not process empty lines correctly
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util
  • Affected Version: 5.0
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2005-05-13
  • Updated: 2011-02-16
  • Resolved: 2005-07-22
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 6
6 b45Fixed
Related Reports
Relates :  
Relates :  
Description
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

Comments
EVALUATION yes for mustang. ###@###.### 2005-07-15 21:19:32 GMT
15-07-2005