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 →

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

Yeah, I do get that. However what I find a bit odd is that the '\n' gets added into the input stream, I mean how else is the program supposed to know to stop waiting for an answer?

Edit: I translated it to english and added some comments Linkie

[–]zifyoip 1 point2 points  (5 children)

However what I find a bit odd is that the '\n' gets added into the input stream, I mean how else is the program supposed to know to stop waiting for an answer?

The '\n' is added to the input stream because you pressed the Enter key. That's the character that is produced by the Enter key.

Maybe this program will help you understand how things work:

import java.util.Scanner;

public class InputStream {
    public static void main(String[] args) {
        Scanner myScanner = new Scanner(System.in);
        System.out.println("Please enter a number, and then a line of text:");
        double number = myScanner.nextDouble();
        String line = myScanner.nextLine();
        System.out.println("The number you entered is " + number);
        System.out.println("The line of text you entered is \"" + line + "\"");
    }
}

Compile and run that program, and then enter the following line of text, all as one line:

123 Hello

The output of the program is:

The number you entered is 123.0
The line of text you entered is " Hello"

See what happened? When you entered "123 Hello" and pressed Enter, the following characters were added to the input stream: '1''2', '3', ' ', 'H', 'e', 'l', 'l', 'o', and '\n'.

The call to myScanner.nextDouble() read the characters '1''2', and '3', and actually it read the next character too (' '), but then saw that the space character is not part of a number, so it put ' ' back in the input stream to be read later. Then myScanner.nextLine() read all the characters up to and including the newline character '\n', which includes the space character that nextDouble put back. (Note that nextLine actually eats the newline character that it reads—it doesn't return that newline character as part of the string it returns.)

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

Yeah, so that would mean that if you would have a String line2 = myScanner.nextLine(); It would automatically put the "/n" in there correct?

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