Input validation using JOptionPane

ensuring user enters valid data

Page 1 of 1

11 Replies - 9774 Views - Last Post: 19 December 2009 - 10:17 PM Rate Topic: -----

#1 Codebug  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 244
  • Joined: 11-October 09

Input validation using JOptionPane

Posted 19 December 2009 - 05:58 PM

I was reading through my newest java book in the section about dialog boxes and reading input from the keyboard. It showed how to use the JOptionPane class to take a user inputted number like 72 and convert it to an actual integer. I understand that whatever a user enters at a JOption prompt is converted to a string. To convert a number entered in the prompt, you must use the Integer.parseInt method. My question is, if I want to write a program that asks the user to enter a number, any integer value, how can I prevent them from entering anything but an integer value. For example, if the user is prompted to enter an integer value and they enter "dog", I want the program to print an error message and reject what they entered. I've thought about this for a while and haven't come to any solid conclusions. I've come up with a sort of pseudo code but I'm having trouble translating it to java code:

if(user enters appropriate value)
{
perform operation
}

else
{
print an error message and reject user input
}

I hope I've made some sense and you guys can understand what I'm asking.

Thanks for the help.

*Edited: topic title edited

This post has been edited by pbl: 19 December 2009 - 10:16 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Input validation using JOptionPane

#2 Calix_  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 05-July 09

Re: Input validation using JOptionPane

Posted 19 December 2009 - 06:13 PM

try something like this maybe:

int1 = getScannerInput.anInt();

if
int1 = (int || double)

System.out.println("This works");

else

System.out.println("Enter a number");

This post has been edited by Calix_: 19 December 2009 - 06:17 PM

Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10447
  • View blog
  • Posts: 38,690
  • Joined: 27-December 08

Re: Input validation using JOptionPane

Posted 19 December 2009 - 06:35 PM

To do something like this, you will want to use a try-catch block, which are used for error handling. In the try-block, you place code which may throw an error. In the catch-block, you put in code to handle the error in the event that it occurs; and if the error doesn't occur, then this code isn't executed.

Since the Scanner nextInt() method throws an InputMismatchException, you can use a try-catch block here. Like so:
int x = 0;
try{
   x = scannerName.nextInt(); //try to get input
}
catch(InputMismatchException e){
   System.out.println("Invalid input");
}



Using the try-catch block in combination with a loop, you can actually force the input prompt to occur repeatedly until the user enters a valid input.
Was This Post Helpful? 0
  • +
  • -

#4 Codebug  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 244
  • Joined: 11-October 09

Re: Input validation using JOptionPane

Posted 19 December 2009 - 06:38 PM

View PostCalix_, on 19 Dec, 2009 - 05:13 PM, said:

try something like this maybe:

int1 = getScannerInput.anInt();

if
int1 = (int || double)

System.out.println("This works");

else

System.out.println("Enter a number");


Are you sure that will work? I didn't think that you could use int or double in an argument as the compiler would think it was a variable or something else. I could be wrong.

View Postmacosxnerd101, on 19 Dec, 2009 - 05:35 PM, said:

To do something like this, you will want to use a try-catch block, which are used for error handling. In the try-block, you place code which may throw an error. In the catch-block, you put in code to handle the error in the event that it occurs; and if the error doesn't occur, then this code isn't executed.

Since the Scanner nextInt() method throws an InputMismatchException, you can use a try-catch block here. Like so:
int x = 0;
try{
   x = scannerName.nextInt(); //try to get input
}
catch(InputMismatchException e){
   System.out.println("Invalid input");
}



Using the try-catch block in combination with a loop, you can actually force the input prompt to occur repeatedly until the user enters a valid input.


Thanks for that info. Really helpful. Could you explain how I could do this with the JOptionPane prompt?
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10447
  • View blog
  • Posts: 38,690
  • Joined: 27-December 08

Re: Input validation using JOptionPane

Posted 19 December 2009 - 06:42 PM

View PostCodebug, on 19 Dec, 2009 - 09:36 PM, said:

View PostCalix_, on 19 Dec, 2009 - 05:13 PM, said:

try something like this maybe:

int1 = getScannerInput.anInt();

if
int1 = (int || double)

System.out.println("This works");

else

System.out.println("Enter a number");


Are you sure that will work? I didn't think that you could use int or double in an argument as the compiler would think it was a variable or something else. I could be wrong.


You're right- that actually won't work. You're better off using the try-catch as I showed above.

@Calix: You can't test for type using the assignment operator. Java has the instanceof keyword that allows you to test for reference types, but not primitives if that helps.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Input validation using JOptionPane

Posted 19 December 2009 - 07:19 PM

Don't really understand where you are going at
If you are going to use JOptionPane, as your topic title suggests, what are you doing with Scanner ?
Was This Post Helpful? 0
  • +
  • -

#7 Codebug  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 244
  • Joined: 11-October 09

Re: Input validation using JOptionPane

Posted 19 December 2009 - 07:39 PM

View Postpbl, on 19 Dec, 2009 - 06:19 PM, said:

Don't really understand where you are going at
If you are going to use JOptionPane, as your topic title suggests, what are you doing with Scanner ?


I wasn't doing anything with Scanner, macosxnerd101 was talking about it.
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Input validation using JOptionPane

Posted 19 December 2009 - 07:43 PM

View Postmacosxnerd101, on 19 Dec, 2009 - 05:35 PM, said:

To do something like this, you will want to use a try-catch block, which are used for error handling. In the try-block, you place code which may throw an error. In the catch-block, you put in code to handle the error in the event that it occurs; and if the error doesn't occur, then this code isn't executed.

Since the Scanner nextInt() method throws an InputMismatchException, you can use a try-catch block here. Like so:
int x = 0;
try{
   x = scannerName.nextInt(); //try to get input
}
catch(InputMismatchException e){
   System.out.println("Invalid input");
}



Using the try-catch block in combination with a loop, you can actually force the input prompt to occur repeatedly until the user enters a valid input.

Macosxnerd we are talking about JOptionPane
what are you doing with Scanner ?
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10447
  • View blog
  • Posts: 38,690
  • Joined: 27-December 08

Re: Input validation using JOptionPane

Posted 19 December 2009 - 08:38 PM

I skimmed over the OP and saw Calix talking about Scanner, so I thought we were talking about Scanner (and that the title was poorly worded). Anyways, I should have read the OP more thoroughly. Sorry about the confusion.

Anyways, same concept but slightly different steps. The parseInt() method, by the way, throws a NumberFormatException instead of the InputMismatchException from Scanner.
String input = JOptionPane.showInputDialog("enter input");
int x = 0;
try{x = Integer.parseInt(input);}
catch(NumberFormatException e){JOptionPane.showMessageDialog(null, "Invalid input");}



Sorry again about the confusion. My mistake completely. :)

This post has been edited by macosxnerd101: 19 December 2009 - 08:40 PM

Was This Post Helpful? 1
  • +
  • -

#11 Codebug  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 244
  • Joined: 11-October 09

Re: Input validation using JOptionPane

Posted 19 December 2009 - 09:31 PM

View Postmacosxnerd101, on 19 Dec, 2009 - 07:38 PM, said:

I skimmed over the OP and saw Calix talking about Scanner, so I thought we were talking about Scanner (and that the title was poorly worded). Anyways, I should have read the OP more thoroughly. Sorry about the confusion.

Anyways, same concept but slightly different steps. The parseInt() method, by the way, throws a NumberFormatException instead of the InputMismatchException from Scanner.
String input = JOptionPane.showInputDialog("enter input");
int x = 0;
try{x = Integer.parseInt(input);}
catch(NumberFormatException e){JOptionPane.showMessageDialog(null, "Invalid input");}



Sorry again about the confusion. My mistake completely. :)


Its quite alright. No worries. You have been a huge help. Thanks! :)

p.s. For future reference on my part, how could I more clearly and concisely word my titles? I thought that my title was fairly descriptive but nonetheless, I want to make sure everyone can understand what I'm asking.
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10447
  • View blog
  • Posts: 38,690
  • Joined: 27-December 08

Re: Input validation using JOptionPane

Posted 19 December 2009 - 10:00 PM

For a new member, this was a pretty good title. If it was me, I would have named it something like "input validation." However, the miscommunication occurred when Calix started talking about Scanner off the wall, which threw me off. Again, I apologize for the confusion, and I'm glad I could be of assistance. :)
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Input validation using JOptionPane

Posted 19 December 2009 - 10:17 PM

View Postmacosxnerd101, on 19 Dec, 2009 - 09:00 PM, said:

For a new member, this was a pretty good title. If it was me, I would have named it something like "input validation." However, the miscommunication occurred when Calix started talking about Scanner off the wall, which threw me off. Again, I apologize for the confusion, and I'm glad I could be of assistance. :)

Done my friend :^:
And thanks for your quick fix
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1