shree
shree

Reputation: 21

Integer.parseInt(string) is giving me number format exception

Why does this code throw NumberFormatException?

int a = Integer.parseInt("1111111111111111111111111111111111111111");

How to get the value of int for that String?

Upvotes: 2

Views: 5385

Answers (3)

recursive
recursive

Reputation: 86154

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.

Upvotes: 1

rgettman
rgettman

Reputation: 178333

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.

Upvotes: 12

AllTooSir
AllTooSir

Reputation: 49432

This is because the number string is pretty large for an int . Probably this requires a BigInteger .

Upvotes: 2

Related Questions