aspose file tools*
The moose likes Beginning Java and the fly likes Scanner question about hasNext() and next() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Scanner question about hasNext() and next()" Watch "Scanner question about hasNext() and next()" New topic
Author

Scanner question about hasNext() and next()

David Maddox
Greenhorn

Joined: Feb 25, 2004
Posts: 12
I am trying to write a program that demonstrates a linked list (yes it is homework). In the specs, the requirement is that the user is supposed to type the end of file character to signify when they are done entering names. I can get it to run perfectly if I have the user type in a signifying phrase ("DONE"), but I cant figure out the end of file part.


Here is the whole program:




Specifically, here is the part where I am reading in their input:




Any suggestions to point me in the right direction would be massively helpful.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

David Maddox wrote:... I can get it to run perfectly if I have the user type in a signifying phrase ("DONE"), but I cant figure out the end of file part...

An end-of-file character should not translate to any (otherwise) valid character. I'm guessing your issue has to do with treating the input as a String rather than a value. I think if you look at the numeric character value beginning each token, you will be able to detect your end-of-file character (which I'm guessing has an ASCII value of 4 or 26).


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Kurt Van Etten
Ranch Hand

Joined: Sep 07, 2010
Posts: 98
I was curious and tried this out on my system (Windows XP) and found that I could enter Ctrl-Z to cause an end-of-file condition on System.in. For example, if I use the code:

then I can terminate the input by entering Ctrl-Z on a line by itself. Interestingly, if I enter other characters before the Ctrl-Z, then it gets read in as an ASCII 26 character. I suspect this behavior may be rather system dependent, though. On Unix you would want to enter Ctrl-D instead of Ctrl-Z, but I wonder if this also holds for Apple?

Even if you get this part to work, though, it has a big drawback--once you have an end-of-file condition on System.in, you will no longer be able to get further input from the keyboard. This means that there would be no way for you to get a response to your menu in the remainder of your program.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Yes, you do use ctrl-D on Apples Unix and Linux (as far as I know).
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Kurt Van Etten wrote:...I suspect this behavior may be rather system dependent, though. On Unix you would want to enter Ctrl-D instead of Ctrl-Z, but I wonder if this also holds for Apple? ...

Yes, Unix uses Ctrl+D rather than Windows' Ctrl+Z, which is why I suggested numeric values of 4 or 26.

Also note this from Wikipedia's End-of-file entry...
In UNIX and AmigaDOS, the translation of the keystroke to EOF is performed by the terminal driver, so a program does not need to distinguish terminals from other input files. By default, the driver converts a Control-D character at the start of a line into an end-of-file indicator. To insert an actual Control-D (ASCII 04) character into the input stream, the user precedes it with a "quote" command character (usually Control-V).

Kurt Van Etten wrote:... Even if you get this part to work, though, it has a big drawback--once you have an end-of-file condition on System.in, you will no longer be able to get further input from the keyboard. This means that there would be no way for you to get a response to your menu in the remainder of your program.

I admit I'm not sure of the "best" way to handle this. One option would be when an end-of-file value is detected, call close() on the Scanner and set a flag to break out of the loop...

I've confirmed this works, but is there a better way...?
Kurt Van Etten
Ranch Hand

Joined: Sep 07, 2010
Posts: 98
marc weber wrote:I admit I'm not sure of the "best" way to handle this. One option would be when an end-of-file value is detected, call close() on the Scanner and set a flag to break out of the loop...


There's not really a need for any additional checks for EOF in my code example--the only way to break out of the input loop is to cause an EOF condition. But is there any way to resume getting input from the keyboard in such a situation? I'm thinking it's not possible, because it wouldn't make sense in some cases, such as if input has been redirected to come from a file. I guess my point is that the requirement in the original post that an EOF be used instead of a sentinel value in the input doesn't seem to be reasonable. On the other hand, if the user is supposed to enter a quoted EOF character, then that is no different from any other sentinel value, and I think I'd want to have the user enter something a little easier to type in.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Kurt Van Etten wrote:... I guess my point is that the requirement in the original post that an EOF be used instead of a sentinel value in the input doesn't seem to be reasonable. On the other hand, if the user is supposed to enter a quoted EOF character, then that is no different from any other sentinel value, and I think I'd want to have the user enter something a little easier to type in.

Exactly.

David, are you making progress, and is any of this helping? Do you have any clarification on this part...?
... In the specs, the requirement is that the user is supposed to type the end of file character to signify when they are done entering names...
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Kurt Van Etten wrote:... There's not really a need for any additional checks for EOF in my code example--the only way to break out of the input loop is to cause an EOF condition...

So you're entering each item as a separate line -- or at least terminating by entering the EOF char on its own line, right? I see how that works.

I was taking the EOF char to be the last token in a single line of input. In that case, I needed a flag to break out of the loop because hasNext() remains true for the EOF char.

Again, I might be misinterpreting the requirements, or maybe just missing something obvious...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
I ought to have looked at your quoted code earlier.
What is !input.hasNext("DONE") supposed to do? It looks an awkward bit of code, which will stop the loop if you enter DONE, but isn't using end-of-file.
You are using != to compare Strings for equality. The != operator is not intended to compare equality of objects; it will almost certainly return misleading results, and you ought to use the equals() method instead.
 
 
subject: Scanner question about hasNext() and next()