Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Forcing Input With The Scanner Class

 
Jesse Kelm
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try changing next() to nextLine() method.

 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48984
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never write == false. It should read while (!isValid) ...
 
Jesse Kelm
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic