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

How to get user to enter correct format number with Scanner

 
Gihan Madushanka
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HELP !!!
I have another question. Assume that a user entered a bad data.(Eg "rat"). It is not an integer. So that program throws an exception. I want that user to enter a number again.
How do I extend the above program to ask the user to enter a number again when he or she has already entered a bad input.
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create a loop that exits once the user has entered valid data.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48954
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gihan Madushanka wrote: . . . I have another question. . . .
New question, therefore new thread. Fortunately I can create a new thread from your old post.

I don’t know. Not at all. Well, maybe I did know once. Maybe if you search my posts for “utility class scanner hasNextInt”, maybe you will find something like this. I have quite forgotten what it said, but you might find that helpful
 
Gihan Madushanka
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:Create a loop that exits once the user has entered valid data.

Where should I create while loop.
Is it in catch block?
Can you write the code please. Only the loop part. So it will be easier for me to understand. (if you don't mind only)
 
Campbell Ritchie
Sheriff
Pie
Posts: 48954
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you read the links I gave you?
 
Gihan Madushanka
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Did you read the links I gave you?

I clicked the links. But I could not find a thread similar to my question.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48954
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not even the last one?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15281
39
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gihan Madushanka wrote:Where should I create while loop.
Is it in catch block?

Think about it. What steps do you want the program to take? Where do you want it to go when the input is not a number? You might want to use a variable to remember if the input was OK or not, so that in the loop condition you can use that to check if the loop should iterate again or not.

Have a look at The while and do-while Statements in Oracle's Java Tutorials. Make sure you understand exactly how loops work - they are very common in programs, so it's good to know exactly how to use them.
 
Gihan Madushanka
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Did you read the links I gave you?

Those examples are kind of complicated.
 
Gihan Madushanka
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:
Gihan Madushanka wrote:Where should I create while loop.
Is it in catch block?

Think about it. What steps do you want the program to take? Where do you want it to go when the input is not a number? You might want to use a variable to remember if the input was OK or not, so that in the loop condition you can use that to check if the loop should iterate again or not.

Have a look at The while and do-while Statements in Oracle's Java Tutorials. Make sure you understand exactly how loops work - they are very common in programs, so it's good to know exactly how to use them.

Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic