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: 71
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: 39547
    
  27
Create a loop that exits once the user has entered valid data.


Ping & DNS - updated with new look and Ping home screen widget
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
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: 71
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: 36486
    
  16
Did you read the links I gave you?
Gihan Madushanka
Ranch Hand

Joined: Apr 09, 2013
Posts: 71
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: 36486
    
  16
Not even the last one?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

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 7 API documentation
Scala Notes - My blog about Scala
Gihan Madushanka
Ranch Hand

Joined: Apr 09, 2013
Posts: 71
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: 71
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
 
Similar Threads
Problem with InputMismatchException in Java
Average calculator without sentinel. A little adjustment needed.
Exit this Do-While w/out an int?
Problem recovering from InputMismatchException in Scanner class