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.
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.
Java Integer maximun value is 2^31-1=2147483647
You should use Long.valueof()
Your String representation is too big (>Integer.MAX_VALUE) for parsing to an int. Try a long instead.
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.
精彩评论