Scanner class skips over whitespace

Use Scanner's hasNextLine() and nextLine() methods and you'll find your solution since this will allow you to capture empty or white-space lines.


You have to understand what is a token. Read the documentation of Scanner:

A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace.

You could use the nextLine() method to get the whole line and not "ignore" with any whitespace.

Better you could define what is a token by using the useDelimiter method.


By default, a scanner uses white space to separate tokens.

Use Scanner#nextLine method, Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line.

To use a different token separator, invoke useDelimiter(), specifying a regular expression. For example, suppose you wanted the token separator to be a comma, optionally followed by white space. You would invoke,

scanner.useDelimiter(",\\s*");

Read more from http://docs.oracle.com/javase/tutorial/essential/io/scanning.html

Tags:

Java

Arrays