• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to Validate User Input in an Input Dialog Box

 
Damien O Sullivan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I need to validate an input box so that the user can enter only round numbers.

The user can't enter any other characters except 0 to 9, they shouldn't be able to enter decimal points either.

They would be answering questions like, 'What is 7 times 8?'

Also, I need to validate it so that the program doesnt crash if they leave the input box blank or empty.

Thanks in advance for any help you can give me.
 
Damien O Sullivan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you need the code, here it is:

 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Damien O Sullivan wrote:Also, I need to validate it so that the program doesnt crash if they leave the input box blank or empty.

Your program is crashing because you don't catch the exception thrown by the Integer.parseInt method.
You can't add validation handling to the JOptionPane dialog, so the only way to validate the input (both empty boxes and invalid numbers) is to catch that exception or write your own input dialog that you can add validation to. The first option is the easiest.
 
Damien O Sullivan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:
Damien O Sullivan wrote:Also, I need to validate it so that the program doesnt crash if they leave the input box blank or empty.

Your program is crashing because you don't catch the exception thrown by the Integer.parseInt method.
You can't add validation handling to the JOptionPane dialog, so the only way to validate the input (both empty boxes and invalid numbers) is to catch that exception or write your own input dialog that you can add validation to. The first option is the easiest.


Should I place the Try on Line 33?

Also, in the Catch, could I get the program to repeat the same question?

For example, if the question was 'What is 5 times 2?' and the user entered 'Ten'...when the program catches that exception, is it possible to repeat the same question?

Or would the program generate a new set of random numbers?

Thanks.
 
Knute Snortum
Sheriff
Posts: 3253
82
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're in a do/while loop, so put something in the catch block that will continue the loop.

Other observations about your code:

* Don't start variable names with a capital letter, doThis instead of DontDoThis.



* You don't need the outer parentheses in the line above.

* You have an extra set of braces inside your do/while loop

* Your formatting, especially your indentation, is inconsistent
 
Damien O Sullivan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:You're in a do/while loop, so put something in the catch block that will continue the loop.

Other observations about your code:

* Don't start variable names with a capital letter, doThis instead of DontDoThis.



* You don't need the outer parentheses in the line above.

* You have an extra set of braces inside your do/while loop

* Your formatting, especially your indentation, is inconsistent



What can I put into the Catch Block that will jump back into the loop? I've tried setting a new variable but it still just ends the program.
 
Tanuj Srivastava
Greenhorn
Posts: 2
Java Java ME Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you should try String Pattern matching before converting StudentGuess into Integer value.
If you get a '.' or any other character other than numeric digits, you can show a message for user to enter new value.

Or,

If you want to only forbid fractional values, you can try using "contains(CharSequence cs)" method of java.lang.String class.

like...

if(StudentGuess.contains("."){
//show a message
}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!