Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JOptionPane creating error message for invalid input

807601Feb 25 2008 — edited Feb 25 2008
I'm extremly new at java so please excuse my ignorance I am sure there is some form of built in method to do this.
I am attempting to make an error message using JOptionPane. The following code works fine as long as an integer is put into the dialog box.
String numString = JOptionPane.showInputDialog(null,
   "How many values would you like to swap?" ,
   "Enter a number of values for array to hold", JOptionPane.QUESTION_MESSAGE);

 // Convert string into integer
int number = Integer.parseInt(numString);
But I am attempting to catch invalid input such as:
1. the user hits enter without typing anything ( )
2. a character value is entered instead of a integer (du)
3. a space is acidently inserted into a valid integer ( 8 8

When invalid input is typed I would like to display an error message. I have attempted many different approaches but all has failed but I am certian that there must be some way of doing this

This is the code I was hopeing would work but it doesn't
String numString = JOptionPane.showInputDialog(null,
   "How many values would you like to swap?" ,
   "Enter a number of values for array to hold", JOptionPane.QUESTION_MESSAGE);
    
if (Integer.parseInt (numString) == null)
   JOptionPane.showMessageDialog(null,
    "invalid entry",
    "invalid entry",JOptionPane.INFORMATION_MESSAGE);

else
 // Convert string into integer
int number = Integer.parseInt(numString);
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 24 2008
Added on Feb 25 2008
7 comments
3,792 views