What is the difference between next() and nextLine() | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 3

What is the difference between next() and nextLine()

In java

6th Aug 2017, 6:33 PM
Akansha Nigam
Akansha Nigam - avatar
4 Antworten
+ 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).
6th Aug 2017, 6:38 PM
Dev - avatar
+ 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.
6th Aug 2017, 6:41 PM
Rrestoring faith
Rrestoring faith - avatar
+ 4
@Dayve @Rrestoring @Matthias Thank you for sharing!
6th Aug 2017, 8:04 PM
Manual - avatar
+ 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.
6th Aug 2017, 7:16 PM
Matthias W