Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Testing user input for integer data type?  RSS feed

 
Brandt Charles
Ranch Hand
Posts: 57
Chrome IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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 ]
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • 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.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this case "regex magic" is easy

[ August 05, 2006: Message edited by: Garrett Rowe ]
 
Amit Batra
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • 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
  • 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");
}
}
}
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • 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
Chrome IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!