Greetings,
I’m trying to validate whether my integer is null. If it is, I need to prompt the user to enter a value. My background is Perl, so my first attempt looks like this:
int startIn = Integer.parseInt (startField.getText()); if (startIn) { JOptionPane.showMessageDialog(null, 'You must enter a number between 0-16.','Input Error', JOptionPane.ERROR_MESSAGE); }
This does not work, since Java is expecting boolean logic.
In Perl, I can use ‘exists’ to check whether hash/array elements contain data with:
@items = ('one', 'two', 'three'); #@items = (); if (exists($items[0])) { print 'Something in \@items.\n'; } else { print 'Nothing in \@items!\n'; }
Is there a way to this in Java? Thank you for your help!
Jeremiah
P.S. Perl exists info.
parseInt()is just going to throw an exception if the parsing can’t complete successfully. You can instead useIntegers, the corresponding object type, which makes things a little bit cleaner. So you probably want something closer to:Beware if you do decide to use
parseInt()!parseInt()doesn’t support good internationalization, so you have to jump through even more hoops: