Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Scanner input under a certain number?

 
Ranch Hand
Posts: 41
  • 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: 218
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: 69403
276
  • 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: 53
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Sheriff
Posts: 7108
184
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"?
 
This guy is skipping without a rope. At least, that's what this tiny ad said:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic