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


I wrote a scanner utility to parse a template. Consider the code snippet:

  string input = FileUtils.readFileToString (new file ("input file path")); Scanner scanner = new scanner (input); Scanner.useDelimiter (System.getProperty ("line.separator")); Println ("checking"); While (scanner.hasNext ()) {System.out.print (scanner.hasNext ("\\ s * # [^ \\ n] *")); System.out.println (":" + scanner.nextLine ()); }  

Input file contents:

  # line 1 ##################### ## # Check # matched with houses # # End of file  

Note: The end of the file line is not present in the input.

Production Production:

  Checking the truth: # Line 1 is true: ################ ###### True: True: # Be True: With #pinned spaces: # false: Wrong:  

My question is why is the third line The next () return is correct when it does not start with '#'?
Any help would be appreciated.

because:

The return is true if the next token is specified by the string Matched pattern matches

There is no token in the blank line, so it is getting the next token from line 4.


Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -