Java – why this numberformatexception?

I have this stack trace (part)

Servlet.service() for servlet action threw exception
java.lang.NumberFormatException: For input string: "37648"
 at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
 at java.lang.Long.parseLong(Long.java:403)
 at java.lang.Long.valueOf(Long.java:482)
 at java.lang.Long.decode(Long.java:593)

In one of my log files, I don't know what the real input string is But the user has the same stack trace

How does this stack trace happen?

Solution

It may be because their input is ahead of zero

This works normally:

public class DecodeLong
{
    public static final void main(String[] params)
    {
        long    l;

        l = Long.decode("37648");
        System.out.println("l = " + l);
    }
}

But if you change this:

l = Long.decode("37648");

To this:

l = Long.decode("037648");

... it becomes invalid octal, and long Exception of parselong does not include leading zeros:

Exception in thread "main" java.lang.NumberFormatException: For input string: "37648"
        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)
        at DecodeLong.main(DecodeLong.java:24)

It does not include it because the decoding call parselong has no zero, but the cardinality is set to 8

Talk about Fuzziness: -) so if you update your program to handle exceptions by displaying actual input, you may find that it follows these directions

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>