This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Scanner Issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Scanner Issue" Watch "Scanner Issue" New topic
Author

Scanner Issue

Terry Tucker
Greenhorn

Joined: Jan 21, 2013
Posts: 22

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

Joined: Feb 06, 2012
Posts: 219

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??


OCPJP
Terry Tucker
Greenhorn

Joined: Jan 21, 2013
Posts: 22

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

Joined: Oct 13, 2005
Posts: 37923
    
  22
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

Joined: Jan 21, 2013
Posts: 22

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Scanner Issue
 
Similar Threads
Overwriting Objects in ArrayList
Exception Handling using scanner to get user input
end of file indicator for more than one file
Scanner question about hasNext() and next()
Scanner class