Win a copy of Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Scanner input under a certain number?

 
Greenhorn
Posts: 7
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I limit the user input? Lets say I have to input the age of a dog, but the input must be under 20. If the input is over 20, then i have to put in another number.
 
Ranch Hand
Posts: 186
5
MS IE Notepad Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sry - can't resist:

take a piece of paper and note down how a conversation between two persons would happen if one asks the other about the age of the dog

note: why shouldn't dogs be older than 20 years?
 
Marshal
Posts: 65057
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Search my posts for utility class Scanner; you will find quite a lot of them. Some will have a utility class in, which uses a Scanner to read from the keyboard. I don't think you will find a complete class anywhere, but only parts of it. Some of those parts will show you how to use a loop to accept only numbers in a predefined range.
 
Ranch Hand
Posts: 38
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Sheriff
Posts: 6110
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
joseph hu: in the Beginning Java forum, we usually wait for the OP to post their code before posting ours.  This is because this forum is for learning, not just getting answers.

That said, what happens if the user of your code enters "a"?
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!