Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to have an error message pop up if user types in more than three integer values?  RSS feed

 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everybody!

New to the forum. Got a n00bie question here. I want to an error message to run if the user types in more than three integers, but I don't know how to do that. Here is my code so far:



Thank you
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I don't think you can. A Scanner continues reading until it has returned from all its methods. If I enter
123 234 345 456
the 456 will simply be ignored.
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it is also unclear from the original message (since no example of acceptable and unacceptable input is provided) whether he really means three integers (like 12345 7 888976543468 being acceptable but 1 2 3 4 is not) or three digits (345 is ok but 8456 is not)
 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry should have been more specific. What I want to have happen is if the user types three numbers no matter how long. Say 30000 44955 32423, if the user puts another number say 25 making it 30000 44955 32423 25 I want an error message to run when the user presses enter nstead of the program itself.
 
Knute Snortum
Sheriff
Posts: 4091
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only way I can think to do this is get the three numbers, then get the rest of the input with nextLine() and test it for non-emptiness.
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even that might not work. If you enternextLine will return the empty String and the 456 will not be noticed.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!