Thursday 15 May 2014

java - Scanner hasNext(String pattern) falsely returns true -



java - Scanner hasNext(String pattern) falsely returns true -

i have written scanner utility parsing template. consider code snippet :

string input = fileutils.readfiletostring(new file("input file path")); scanner scanner = new scanner(input); scanner.usedelimiter(system.getproperty("line.separator")); system.out.println("checking"); while(scanner.hasnext()) { system.out.print(scanner.hasnext("\\s*#[^\\n]*")); system.out.println(" : " + scanner.nextline()); }

input file contents :

# line 1 ####################### # check # matched spaces # // end of file

note : end of file line not nowadays in input.

output produced :

checking true : # line 1 true : ####################### true : true : # check true : # matched spaces true : # false : false :

my question why 3rd line hasnext() returns true when not start '#' ? help appreciated.

because hasnext:

returns true if next token matches pattern constructed specified string.

the blank line contains no tokens finding next token line 4.

java regex java.util.scanner

No comments:

Post a Comment