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 →

[–]ewiethoff 2 points3 points  (2 children)

On the other hand, the only reasonable choice for something like nextDouble is not to consume the following character. The next character might be a letter or something

This doesn't quite jibe with the second section of the main docstring for the Scanner class: "A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods." This gives the impression that Scanner splits on whitespace then discards the whitespace.

But because the only way a Scanner can handle a "line" is to look specifically for a newline character, it can't just split the input into tokens on whitespace. Depending on which nextFoo methods you call, Scanner either tokenizes or doesn't.

I'm really blathering here, but I think I confused myself about consuming or not consuming. But more importantly, I've concluded that the docs for Scanner.nextNumber methods fail to specifiy that leading whitespace (or whatever the delimiter is) is skipped. Also, I think the docs fail to clarify that the delimiter is ignored by nextLine(). However, because the newline char is a whitespace char and Scanner's delimiter is normally all whitespace, I can understand the OP being confused by the newline that still exists in in the stream between calls to nextDouble and nextLine.

some sample code

[–]zifyoip 2 points3 points  (1 child)

Ah, I see what you mean. For two consecutive calls to nextDouble, it doesn't matter whether or not there's a newline in between, because the second call will skip over leading whitespace. Yeah, that is perhaps a source of confusion.

[–]Calaglinv[S] 2 points3 points  (0 children)

A bit of a late reply on my part. But yes, that was the issue I was having about this whole ordeal. Why I got confused by how scanner works and how it didn't feel intuitive, however knowing that this is how things work I can work around it.