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);