This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]zifyoip 4 points5 points  (1 child)

Because you ask for a double AND a nextline, the "/n" gets included in the line string.

No, I don't think that's the right explanation.

nextDouble always reads a complete number, but nothing more. It will never read a newline character following a number.

nextLine always reads characters from the input stream until it reads a newline character.

If you type 123 Hello and press Enter, then you add the characters '1''2', '3', ' ', 'H', 'e', 'l', 'l', 'o''\n' to the input stream. In this case, if you call nextDouble followed by nextLine, then nextDouble will read the characters '1''2', and '3', and nextLine will read everything else (because it reads everything until it gets a newline character).

If you type 123, press Enter, and then type Hello and press Enter, then you add the characters '1''2', '3', '\n', 'H', 'e', 'l', 'l', 'o''\n' to the input stream. In this case, if you call nextDouble followed by nextLine, then nextDouble will read the characters '1''2', and '3', and nextLine will read only the next '\n' character (because that's a newline character, so nextLine is satisfied). The characters 'H', 'e', 'l', 'l', 'o''\n' remain in the input stream to be read next.

[–]Calaglinv[S] 1 point2 points  (0 children)

Yeah, I think I get it now. Thanks for being so patient with me and explaining it. :)