What is the main difference between
My main goal is to read the all text using a
Scanner which may be “connected” to any source (file for example).
Which one should I choose and why?
I always prefer to read input using
nextLine() and then parse the string.
next() will only return what comes before the delimiter (defaults to whitespace).
nextLine() automatically moves the scanner down after returning the current line.
A useful tool for parsing data from
nextLine() would be
String data = scanner.nextLine(); String pieces = data.split("\s+"); // Parse the pieces
For more information regarding the Scanner class or String class refer to the following links.