• 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
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Scanner input under a certain number?  RSS feed

 
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: 155
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: 64179
215
  • 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: 35
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Sheriff
Posts: 5931
155
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • 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"?
 
I wish to win the lottery. I wish for a lovely piece of pie. And I wish for a tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!