Cjen1 Cjen1 - 11 months ago 33
Java Question

Number format exception on input of 0x20A0600000000000L into Long.decode

I'm trying to take an input of a hexidecimal Long into a program and values which seem like they should work:

cause a java.lang.NumberFormatException, it says that the input string is

Exception in thread "main" java.lang.NumberFormatException: For input string: "20A0600000000000L"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Long.parseLong(Unknown Source)
at java.lang.Long.valueOf(Unknown Source)
at java.lang.Long.decode(Unknown Source)

This is how I'm calling it:
long temp = Long.decode(args[0]);

This also occurs using a different online compiler: https://www.compilejava.net with the previously stated value input as a constant, i.e.

As a few asides, this is part of a course and the value is the input that they gave, hence its appearance.

Answer Source

Suffix L in Java lets you distinguish numeric literals of type long from numeric literals of type int. Since decode is a method of Long, it knows that it is parsing a long number, so it does not accept trailing L suffix. Removing it fixes the problem:

long lv = Long.decode("0x20A0600000000000");