• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Scanner question about hasNext() and next()

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you do use ctrl-D on Apples Unix and Linux (as far as I know).
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic