This week's book giveaway is in the Testing forum.We're giving away four copies of Data Structures the Fun Way: An Amusing Adventure with Coffee-Filled Examples and have Jeremy Kubica on-line!See this thread for details.
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:
• Campbell Ritchie
• Liutauras Vilda
• Tim Cooke
• Jeanne Boyarsky
• Paul Clapham
Sheriffs:
• Devaka Cooray
• Ron McLeod
• paul wheaton
Saloon Keepers:
• Tim Moores
• Piet Souris
• Tim Holloway
• Stephan van Hulst
• Carey Brown
Bartenders:
• Al Hobbs
• Frits Walraven
• Scott Selikoff

# Check for the given String is a integer

Greenhorn
Posts: 1
• Number of slices to send:
Optional 'thank-you' note:
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.

Sheriff
Posts: 14691
16
• Number of slices to send:
Optional 'thank-you' note:

Ranch Hand
Posts: 5575
• Number of slices to send:
Optional 'thank-you' note:

Greenhorn
Posts: 2
• Number of slices to send:
Optional 'thank-you' note:
brother thats a long not a integer..

Greenhorn
Posts: 21
• Number of slices to send:
Optional 'thank-you' note:

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");
}

Ranch Hand
Posts: 198
• Number of slices to send:
Optional 'thank-you' note:
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.

Ranch Hand
Posts: 177
• Number of slices to send:
Optional 'thank-you' note:
I'd use the org.apache.commons.lang.StringUtils isNumeric(String) method.

StringUtils.isNumeric("1001234578999") returns true.

Ranch Hand
Posts: 37
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
(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?)

Marshal
Posts: 76842
366
• Number of slices to send:
Optional 'thank-you' note:
[Pedantic mode]The Strings quoted have all been integers. Maybe not ints.[/Pedantic mode]

 I wasn't selected to go to mars. This tiny ad got in ahead of me: the value of filler advertising in 2021 https://coderanch.com/t/730886/filler-advertising