• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Testing user input for integer data type?

 
Ranch Hand
Posts: 57
IntelliJ IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this case "regex magic" is easy

[ August 05, 2006: Message edited by: Garrett Rowe ]
 
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you could convert the primitive to the wrapper equiv and then use the instanceof test.
 
Amit Batra
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry I didnt read that you wanted an exception which means your value will be coming in from runtime.

class A{
public static void main(String[] args){
try{
int b=new Integer(Integer.parseInt(args[0]));

}catch(Exception e){
System.out.println("An exception has been thrown");
}
}
}
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Layne
 
Brandt Charles
Ranch Hand
Posts: 57
IntelliJ IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic