Why does this code throw NumberFormatException
?
int a = Integer.parseInt("1111111111111111111111111111111111111111");
How to get the value of int
for that String
?
Why does this code throw NumberFormatException
?
int a = Integer.parseInt("1111111111111111111111111111111111111111");
How to get the value of int
for that String
?
The value that you're attempting to parse is much bigger than the biggest allowable int
value (Integer.MAX_VALUE
, or 2147483647
), so a NumberFormatException
is thrown. It is bigger than the biggest allowable long
also (Long.MAX_VALUE
, or 9223372036854775807L
), so you'll need a BigInteger
to store that value.
BigInteger veryBig = new BigInteger("1111111111111111111111111111111111111111");
From BigInteger
Javadocs:
Immutable arbitrary-precision integers.
This is because the number string is pretty large for an int
. Probably this requires a BigInteger
.
There is no integer value for that string. That's why it's throwing an exception. The maximum value for an integer is 2147483647, and your value clearly exceeds that.