This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

validate user input

 
Amy Hurt
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic