United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-6437114 Change in java.util.Scanner.findInLine(".") behavior
JDK-6437114 : Change in java.util.Scanner.findInLine(".") behavior

Details
Type:
Bug
Submit Date:
2006-06-10
Status:
Closed
Updated Date:
2010-04-02
Project Name:
JDK
Resolved Date:
2006-06-15
Component:
core-libs
OS:
generic
Sub-Component:
java.util
CPU:
generic
Priority:
P3
Resolution:
Not an Issue
Affected Versions:
6
Fixed Versions:

Related Reports
Relates:

Sub Tasks

Description
Reference this forum posting:

http://forums.java.net/jive/thread.jspa?threadID=16058&tstart=0


If I compile and execute the following program FROM THE WINDOWS COMMAND LINE, I get different behavior under Java 6 than I did under Java 5. (With Java 6, I get a NullPointerException; with Java 5, I didn't.)

import java.util.Scanner;

public class TestScan {

    public static void main(String args[]) {
	Scanner keyboard = new Scanner(System.in);

	System.out.print("Number: ");
	int i = keyboard.nextInt();
	System.out.print("char: ");
	char c = keyboard.findInLine(".").charAt(0);
    }
}
Teasing apart the submitted test case shows that java.util.Scanner.findInLine(".") is returning null where it returned a String in earlier releases:

import java.util.Scanner;

public class TestScan {

    public static void main(String args[]) {
	Scanner keyboard = new Scanner(System.in);

	System.out.print("Number: ");
	int i = keyboard.nextInt();
	System.out.print("char: ");
	String s = keyboard.findInLine(".");
	if (s != null) {
	    char c = s.charAt(0);
	    System.out.println("c is: " + c);
	} else {
	    System.out.println("s is null.");
	}
    }
}

% java -showversion TestScan
java version "1.6.0-ea"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.6.0-ea-b45)
Java HotSpot(TM) Client VM (build 1.6.0-ea-b45, mixed mode)

Number: 8
char: s is null.

                                    

Comments
EVALUATION

The "behavior change" is the expected result of bug fix for 6269946. After the
invoke of "keyboard.nextInt()", the line terminator/newline character(s)is still
in the input stream buffer, so the correct result of keyboard.findInLine("."),
as the spec says, should return "null", since there is NOTHING to match pattern
"." between current position and the next line terminator. The behavior of 5.0
and earlier are incorrect, we fixed it in 6.0.
                                     
2006-06-15



Hardware and Software, Engineered to Work Together