• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Scanner Issue

 
Terry Tucker
Greenhorn
Posts: 24
Linux Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings:

I have been working on some code that writes objects to a file. I am using a Scanner object to process the menu which provides for a number of operations on the objects in the file. One operation, adding a new record, branches into a section of code which loops through various prompts gather input from a user that is necessary for the creation of the record. I want to be able to Ctrl-D or Ctrl-Z out of that loop gracefully and return to the menu. I have two Scanner objects; one for dealing with getting input from the menu, and the other for getting information for the addition of the record. They are both tightly scoped. No matter what I try I cannot get the behaviour I desire. Doing Ctrl-D (Linux), generates a NoSuchElementException. If I do an input.nextLine() to clear out the buffer, I get an exception that shuts the application down. If I simply generate a message to standard err and return, it appears that the buffered input causes the Scanner object that is operating on the menu to capture the same exception as before even though I have trapped it earlier!

Rather than try to show all this code, I would be most happy for an example that does this sort of thing; that is, a Scanner object that manages a menu and another that manages the input for creating a "record". I have spent hours trying to figure out something that will work, but to now avail.

Any ideas on an example? The books that I have only represent a very simply input operation.

Thanks...
 
Ishan Pandya
Ranch Hand
Posts: 226
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Terry Tucker wrote:Greetings:
A Scanner object that manages a menu and another that manages the input for creating a "record". I have spent hours trying to figure out something that will work, but to now avail.
Any ideas on an example? The books that I have only represent a very simply input operation.


As you are writing object into the file have you tried Serialization using FileInput/OutputStream and ObjectInput/OutputStream??
 
Terry Tucker
Greenhorn
Posts: 24
Linux Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ishan Pandya wrote:
Terry Tucker wrote:Greetings:
A Scanner object that manages a menu and another that manages the input for creating a "record". I have spent hours trying to figure out something that will work, but to now avail.
Any ideas on an example? The books that I have only represent a very simply input operation.


As you are writing object into the file have you tried Serialization using FileInput/OutputStream and ObjectInput/OutputStream??


Yes, I am using Serialization and Object Input/Output stream. I am not having any trouble with that. My issue is with Scanner. I want to able to gracefully exit the input loop when putting in a new record by typing a Ctrl-D. Here is a code snippet:


If I leave the code like the above, it bombs on Ctrl-D inside the catch block complaining that there is no nextLine(). If I take the call to in.nextLine() out and simply break out of the loop, the Scanner object that is processing the menu input picks up the same exception (NoSuchElementException) and bombs out there. This is hard to explain.

Thanks for the reply Ishan...
 
Campbell Ritchie
Sheriff
Pie
Posts: 49865
71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are much simpler ways to do that with Scanner, and more elegant.
Go through the methods of Scanner and you will find some predicate methods which help you avoid that Exception. There is also an example rather similar to what I think you want at the beginning of the documentation. Rather similar, by no means very similar.
 
Terry Tucker
Greenhorn
Posts: 24
Linux Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:There are much simpler ways to do that with Scanner, and more elegant.
Go through the methods of Scanner and you will find some predicate methods which help you avoid that Exception. There is also an example rather similar to what I think you want at the beginning of the documentation. Rather similar, by no means very similar.


Thank you sir. I will check that out later this afternoon when I am not on my employer's clock
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic