• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Check for the given String is a integer

 
Prasad Velakanti
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi All,

Trying to check the given String value is a integer. The given String value is a integer "1001234578999" but throwing a number format exception. Can some one shed some light why its throwing number format exception.

 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
welcome to javaranch Prasad
 
Abhi Chavi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
brother thats a long not a integer..
 
Deepika Singh
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

String s = "1001234578999";
try {
long i = Long.parseLong(s);
System.out.println("long value is ::>>"+i);
}
catch (NumberFormatException nfe)
{
System.err.println("s is not parseable as an integer");
}
 
Mohammed Yousuff
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prasad, the number which you are converting is not a integer, the maximum value which Interger supports is Integer.MAX_VALUE.... So that reason exception was thrown in your code.

Check "Deepika Singh" code which is the valid way to parse the big numbers ... i hope this make clear.
 
Max Rahder
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd use the org.apache.commons.lang.StringUtils isNumeric(String) method.

StringUtils.isNumeric("1001234578999") returns true.


 
Jarred Olson
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When parsing Strings of variable (potentially large values) it's better to use Long.parseLong() method to avoid the NumberFormatException:


You might want to wrap it in a try/catch block as well because if the String happens to have a non-numerical character it will throw the same exception.
 
Max Rahder
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(Pradad didn't say he needed to parse the string -- he said he needed to check whether it was an integer. Parsing it, and using exception handling as a way of testing whether it worked, seems to be an awkward way to do that test. If the ultimate goal is to convert it to a numeric value, then we need more information -- perhaps BigInteger is the best way to handle the value?)
 
Campbell Ritchie
Sheriff
Pie
Posts: 49830
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Pedantic mode]The Strings quoted have all been integers. Maybe not ints.[/Pedantic mode]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic