How to use the Java Scanner next & nextLine methods for String Input

preview_player
Показать описание
Ever wonder what the difference was between the Java Scanner's next and nextLine methods?

In this Java Scanner tutorial we show you how to use the Scanner's nextLine and next methods to extract String based user input, while also showing you the key differences between the next and nextLine methods of the Java Scanner class.

Want a quick example of how the Java Scanner next method works, along with a tutorial on the nextLine method of the Java Scanner? This quick example tutorial is for you.
Рекомендации по теме
Комментарии
Автор

Thnks a lot am just a beginner and I was stuck here from last night it was giving me a headache until I found this, You teach really well thnks a lot

suvrabhattacharjee
Автор

OMG, the best tutorial, thankyou so much +Subscriber

cogsbr
Автор

Thank you very much, everything is clear and interesting.

bin
Автор

thank you sooo much!!! you are such a life saver😭😭😭🙏

vantebear
Автор

Thanks a lot. This video is very helpful.

soneryildiz
Автор

Thanks a lot, but how is it even accepting input at 4:08 ? Shouldn't the while condition be false, because we haven't entered any value before it?

Also, I read that the next() method doesn't read the newline character, which can cause problems if we're using the nextLine() method afterwards, so we'll have to empty the buffer. But I don't understand what this "newline character" is.

Consider this code:

Scanner sc = new Scanner(System.in);
firstName = sc.next();
lastName = sc.next(); //No problems so far
fullName = sc.nextLine(); //and it's skipped because sc.next() doesn't read the newline character.

I understand the newline character is "\n", but firstly, where is it???? And secondly, how come there is no problem with the second sc.next() reading the lastName? Isn't there a newline character stuck in the buffer from the first sc.next()?

pdpaxpv