Why not nextString instead of nextLine | Sololearn: Learn to code for FREE!

0

Why not nextString instead of nextLine

im a bit confused why they didn't use nextString? can someone please explain for a noob.

4/18/2017 9:37:58 PM

serdox

6 Answers

New Answer

+3

lets say you have a string String str = "Hello World!"; Scanner scan = new Scanner (str); scan.next (); vs scan.nextLine (); scan.nextLine () would give: Hello World! while scan.next () would give: Hello nextLine gives the whole line, which next only gives the next word

+3

because there is already a method for nextString. but its called next()

+2

yes. the line could include integers and decimal numbers, except it would be returned as a string and in the case of only next (), even if it is an integer, it will be converted to a string, unless you do nextInt ()

0

so does that mean next() would only print the first word of the string?

0

i mean one line can be put in one string no?

0

so the line could also include integers etc? i think i got it. big thanks.😁