File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Forcing Input With The Scanner Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Forcing Input With The Scanner Class" Watch "Forcing Input With The Scanner Class" New topic
Author

Forcing Input With The Scanner Class

Jesse Kelm
Greenhorn

Joined: Sep 06, 2011
Posts: 11
I am trying to figure out how to force a user to input some sort of data at a continue prompt. So if they just press the enter key, it will tell them the entry is required. Here is what I have. It works if they press y or n and gives the error the entry must be y or n if they use a different letter. The code seems to wait after the enter key is pressed with no values. Here is my code:

John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Try changing next() to nextLine() method.

John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
If you see the API Scanner for the next() method, they have mentioned of a delimiter. The default delimiter is a character that satisfies the method Character:isWhiteSpace(). And return key is a white space character. Hence when you enter a multiple return keys there is nothing between them to get a complete token. I would like the moderator to check this answer.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38353
    
  23
Never write == false. It should read while (!isValid) ...
Jesse Kelm
Greenhorn

Joined: Sep 06, 2011
Posts: 11
Thanks much for the help John! That is exactly where the issue was, not sure how I overlooked that one.

Also thanks for the syntax help Campbell!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Forcing Input With The Scanner Class