Integer.parseInt number format exception?

Danny picture Danny · Nov 26, 2013 · Viewed 45.8k times · Source

I feel like I must be missing something simple, but I am getting a NumberFormatException on the following code:

System.out.println(Integer.parseInt("howareyou",35))

Ideone

It can convert the String yellow from base 35, I don't understand why I would get a NumberFormatException on this String.

Answer

René Link picture René Link · Nov 26, 2013

Because the result will get greater than Integer.MAX_VALUE

Try this

System.out.println(Integer.parseInt("yellow", 35));
System.out.println(Long.parseLong("howareyou", 35));

and for

Long.parseLong("abcdefghijklmno",25)

you need BigInteger

Try this and you will see why

System.out.println(Long.MAX_VALUE);
System.out.println(new BigInteger("abcdefghijklmno",25));