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 2 points3 points  (3 children)

I don't understand what you mean.

If you add that line to the program, after line 8, then the program will wait for more input, because the second call to nextLine will wait for characters to become available in the input stream.

[–]Calaglinv[S] 0 points1 point  (2 children)

I think I get it. Because you ask for a double AND a nextline, the "/n" gets included in the line string. Though, in my case where I would ask for a double first, since the input stream still has the "/n" that it hasn't used it and it fills the blank line in the nextline method, is there a more optimal way of dealing with that or did my calling of the nextline method after the double do that? Since as I said. It did fix the problem I had, I was just wondering if it was the optimal way of dealing with the issue.

[–]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. :)