java - What's the difference between next() and nextLine() methods from Scanner class? -


what main difference between next() , nextline()?
main goal read text using scanner may "connected" to source (file example).

which 1 should choose , why?

i prefer read input using nextline() , parse string.

using next() return comes before space. nextline() automatically moves scanner down after returning current line.

a useful tool parsing data nextline() str.split("\\s+").

string data = scanner.nextline(); string[] pieces = data.split("\\s+"); // parse pieces 

for more information regarding scanner class or string class refer following links.

scanner: http://docs.oracle.com/javase/7/docs/api/java/util/scanner.html

string: http://docs.oracle.com/javase/7/docs/api/java/lang/string.html


Comments