aspose file tools*
The moose likes Beginning Java and the fly likes How to get user to enter correct format number with Scanner Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to get user to enter correct format number with Scanner" Watch "How to get user to enter correct format number with Scanner" New topic
Author

How to get user to enter correct format number with Scanner

Gihan Madushanka
Ranch Hand

Joined: Apr 09, 2013
Posts: 78
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
Marshal

Joined: Mar 22, 2005
Posts: 42919
    
  68
Create a loop that exits once the user has entered valid data.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

Joined: Apr 09, 2013
Posts: 78
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

Joined: Oct 13, 2005
Posts: 40052
    
  28
Did you read the links I gave you?
Gihan Madushanka
Ranch Hand

Joined: Apr 09, 2013
Posts: 78
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

Joined: Oct 13, 2005
Posts: 40052
    
  28
Not even the last one?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14430
    
  23

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Gihan Madushanka
Ranch Hand

Joined: Apr 09, 2013
Posts: 78
Campbell Ritchie wrote:Did you read the links I gave you?

Those examples are kind of complicated.
Gihan Madushanka
Ranch Hand

Joined: Apr 09, 2013
Posts: 78
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get user to enter correct format number with Scanner