Let me elaborate, I need input to be in integer format. Any other will need to throw an exception. I went poking around in the Integer class to no avail, but is there a method to test if an element of an array of this input is an integer? I expect it could be done through some regex magic, of which I am not proficient, but was hoping there was an easier way.
Thanks in advance,
Brandt [ August 05, 2006: Message edited by: Brandt Charles ]
Originally posted by Brandt Charles: Let me elaborate, I need input to be in integer format. Any other will need to throw an exception. I went poking around in the Integer class to no avail, but is there a method to test if an element of an array of this input is an integer? I expect it could be done through some regex magic, of which I am not proficient, but was hoping there was an easier way.
Thanks in advance,
Brandt
[ August 05, 2006: Message edited by: Brandt Charles ]
I would set up a try/catch and catch a NumberFormatException.
regex gets tricky if you want to allow plus and minus signs and enforce data ranges. I find it more expressive to use Integer.parseInt() and catch the exception in a little boolean method that does nothing else.
There is a cost to throwing an exception but it takes batches of thousands or millions to matter. If you do this with interactive user input the user will never notice the overhead.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
How does the user input this integer? Are you creating a console interface or a GUI? All the above will work fine if you are using plain text. However, if you are doing a GUI, you might want to look into using JFormattedTextField.
Thanks, everyone. But Garrett and Keith are the winners. I integrated both of your solutions, and it worked splendidly. The matches() method could not have been easier to use.
Thanks again!
Green, green, green!
You would be much easier to understand if you took that bucket off of your head. And that goes for the tiny ad too!
Gift giving made easy with the permaculture playing cards