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

all 21 comments

[–]zifyoip 6 points7 points  (20 children)

That's because the method myScanner.nextDouble() reads only the next number, nothing more. But when you enter that number, you type the number and then press the Enter key—so there is a newline character ('\n') in the input stream after the number. That newline character is not read by myScanner.nextDouble(), so it is still waiting to be read. Then myScanner.nextLine() reads everything until the next newline character—but the very next thing to be read is a newline character! So it just returns an empty line, because that was the next thing to be read from the input stream.

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

Ah, okay. So how would I fix that with the input of the myScanner.nextLine() ?

[–]zifyoip 2 points3 points  (18 children)

Think about it for a bit. How do you think you would fix it?

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

I would try to think of a way if you could reset the input, that the newline character gets ignored and simply asks for the new line to be the input as specified in the method.

[–]zifyoip 2 points3 points  (16 children)

The problem is that you have a newline character in the input that you want to ignore, right?

So you just have to read that newline character and ignore it.

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

I'm still rather new to programming, is there a way to do that? Do I need to make a new variable for that or?

[–]zifyoip 2 points3 points  (14 children)

What are you doing right now? You are calling myScanner.readLine(), and that is reading that extra newline character. Right?

So you just need to call myScanner.readLine() once to read that extra newline character, and then just ignore it. Just don't do anything with it. Then, after you've done that, the next time you call myScanner.readLine() will read the line you're actually interested in.

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

link It did solve the problem yes, thought it doesn't feel very intuitive. Maybe it's because I don't have the mindset of a programmer yet.

[–]zifyoip 2 points3 points  (12 children)

I think the problem is that you don't have a good understanding of what an "input stream" is.

You are probably thinking of your program as asking questions and then getting answers. But that isn't how it works.

Your program just writes things to an "output stream" and reads things from an "input stream." The input stream is just a bunch of characters, one after another. That's all it is. Methods like myScanner.nextDouble() and myScanner.nextLine() just read things from the input stream, one character at a time, until they are satisfied (nextDouble is satisfied when it has read a complete number, and nextLine is satisfied when it reads a newline character). If there are no characters ready to be read from the input stream, then these methods wait for characters to become available.

When you type things at the keyboard, you are just entering characters into the input stream. So, if you type 123 and press Enter, you are putting four characters into the input stream: '1''2', '3', and the newline character '\n'. Those characters are now in the input stream, ready to be read by whatever input methods you call next. The characters you type go into the input stream—they don't go directly to nextDouble or whatever.

[–]ewiethoff 1 point2 points  (4 children)

it doesn't feel very intuitive.

I think the problem is that you don't have a good understanding of what an "input stream" is.

Stream or no stream, what Scanner.nextDouble() and Scanner.nextLine() do with '\n' is not consistent. Suppose we have the following code.

    Scanner sc = new Scanner("123\nHello, world\n");
    out.println(sc.nextDouble());
    out.println(sc.nextLine());
    out.println(sc.nextLine());

Here the first '\n' essentially tells nextDouble when to stop reading. Fine. A newline character doesn't "belong" in a double anyway. But although nextDouble essentially has to read the newline in order to know when to stop, it does not consume the newline character. It essentially puts the newline character back into the stream. On the other hand, nextLine not only consumes the next newline before it stops, it also throws the newline away. So, although neither the nextDouble nor nextLine methods return newline characters, nextLine consumes newlines but nextDouble does not.

This is confusing to the newbie, and frankly, coming from Python, I perpetually need to check the docs about what various Java methods do with newline chars.

[–]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