+ 3
What is the difference between next() and nextLine()
In java
4 Answers
+ 14
If you write "Hello World" in the console:
By using next() you will get the word "Hello" as the input value and you won't get the next word.
If you use nextLine() instead, you will get the whole line ("Hello World" in this case) you have written, since it will read the full line and will move on to the next line.Â
Also, next() places the cursor in the same line after reading the input. nextLine() reads input including space between the words (that is, it reads till the end of line \n).
+ 4
next() returns all characters up to the next space. Moving the stream past the word it returns.
nextLine() returns the entire line of input, moving the stream to the next line.
+ 4
@Dayve
@Rrestoring
@Matthias
Thank you for sharing!
+ 3
you can also call the useDelimiter method on scanner object before using next() that way you can skip through comma delimited input for example.