Federer
Federer

Reputation: 34715

Why does trying to parse this value result in a NumberFormatException?

Code:

String myVar = "1255763710960";
int myTempVar=0;
try
{ 
   myTempVar = Integer.valueOf(myVar);
}
catch (NumberFormatException nfe)
{
    System.out.println(nfe.toString());
}

Output:

java.lang.NumberFormatException: 
For input string: "1255763710960"

I have absolutely no idea why this is.

Upvotes: 2

Views: 1828

Answers (4)

Michael Bavin
Michael Bavin

Reputation: 4014

Your String representation is too big (>Integer.MAX_VALUE) for parsing to an int. Try a long instead.

Upvotes: 3

Joachim Sauer
Joachim Sauer

Reputation: 308001

1255763710960 is more than Integer.MAX_VALUE which is 2147483647, so that value doesn't fit in an int.

You'll need to use a long and Long.valueOf() (or better yet Long.parseLong() to avoid unnecessary auto-unboxing) to parse that value.

Upvotes: 2

Telcontar
Telcontar

Reputation: 4870

Java Integer maximun value is 2^31-1=2147483647

You should use Long.valueof()

Upvotes: 4

John Feminella
John Feminella

Reputation: 311486

The value you're trying to store is too big to fit in an integer. The maximum value for an Integer is 231-1, or about 2 billion. This number exceeds that by several orders of magnitude.

Try using a Long and parseLong() instead.

Upvotes: 9

Related Questions