This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes validate user input Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "validate user input" Watch "validate user input" New topic
Author

validate user input

Amy Hurt
Greenhorn

Joined: Apr 29, 2003
Posts: 1
Hi everyone,
I need to add range-check logic to a program where the user is supposed to enter values between 0 and 100. If they enter 102, I need to display an error. Would I use a Try Catch block for this? I have one Try Catch block in the program already that does this if they enter non-numeric:
//Check for NumberFormatException
int score;
try {
score = Integer.parseInt( args[i] );
numberOfScores++;
sumScores += score;
}catch ( NumberFormatException e ) {
System.out.println( args[i] + " is not a valid score" );
continue;
}

I tried incorporating another catch in there that but only get errors. So maybe Try Catch doesn't work for range checks? Any suggestions? Thanks, Amy
Donald R. Cossitt
buckaroo
Ranch Hand

Joined: Jan 31, 2003
Posts: 401
How about something like the following:

Of course convert args to Integer.parseInt(...). I think you get the idea.
HTH
[ May 21, 2003: Message edited by: Donald R. Cossitt ]

doco
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
hi Amy
Would I use a Try Catch block for this?
Yes you can customize the exception to do it.
But I prefer not to use this method to do.
//Check for CheckForNumberRange

I think it works enough,but if you want to know how to use exception to do the same thing,please let me know.

[ May 21, 2003: Message edited by: siu chung man ]

Francis Siu
SCJP, MCDBA
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Personally, I would use something like Donald suggested. However, I would use a while loop instead of an if statement. That way the program continues to prompt the user for a value until they enter a valid number.


Java API Documentation
The Java Tutorial
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
hi Layne
I agree with you that I see the coding seems to be hiding a loop around the given coding.
Because args[i] which I think should use the loop if index i more than 1
And also,my default ,the coding seems to be needed by the user passing the value in console at runtime---->args[i].
So the answer should be meet the requirement.
But,you said the truth,it is also what I prefer Amy to do it.
That way the program continues to prompt the user for a value until they enter a valid number.
Ar.....
Doco
args[ 0 ] >= 0 || args[ 0 ] <= 100
It should be
args[ 0 ] >= 0 && args[ 0 ] <= 100

[ May 21, 2003: Message edited by: siu chung man ]
Donald R. Cossitt
buckaroo
Ranch Hand

Joined: Jan 31, 2003
Posts: 401
Yeah, I noticed that after I posted but was in my office at the time and the phone rang... well you know... just didn't get back to it. :roll:
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
Amy, do you need a sentinal for this program? Because, use doco's method and use the sentinal to exit the while loop, and the if statement will still work, but if not then use Layne's method with doco's commands.
lol
I'll shut up now
 
wood burning stoves
 
subject: validate user input