tags:

views:

70

answers:

4

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.

+8  A: 

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.

John Feminella
Ah, that makes a lot more sense now. Thanks for that, John! :)
day_trader
+4  A: 

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

You should use Long.valueof()

Telcontar
in addition to changing the type declaration of myTempVar
Gaby
+2  A: 

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.

Joachim Sauer
+3  A: 

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

Michael Bavin