简体   繁体   English

Long.parseLong的NumberFormatException

[英]NumberFormatException for Long.parseLong

when I try to run this Code (yes, I know it will run horribly long and is not very pretty): 当我尝试运行此代码时(是的,我知道它会运行很长时间并且不是很漂亮):

DecimalFormat formatA = new DecimalFormat("000");
        DecimalFormat formatB = new DecimalFormat("0000000000000000000000000000000000000000");

        for(int c=0; c<=7; c++){
            String binC = formatA.format(Integer.parseInt(Integer.toBinaryString(c)));
            for(int d=0; d<LFSRa.length; d++){
                LFSRa[d]=Character.getNumericValue((binC.charAt(d)));                   
            }
            for(long e=0; e<=1099511627775L; e++){
                String binE = formatB.format(Long.parseLong((Long.toBinaryString(e))));
                for(int f=0; f<LFSRb.length; f++){
                    LFSRb[f]=Character.getNumericValue((binE.charAt(f)));
                }


            }
        }

I get the following exception 我得到以下异常

Exception in thread "main" java.lang.NumberFormatException: For input string: "10000000000000000000"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Long.parseLong(Unknown Source)
at java.lang.Long.parseLong(Unknown Source)
at A41.main(A41.java:76)

and I don't get why. 我不明白为什么。 What should I do? 我该怎么办? Could somebody explain the problem to me? 有人可以向我解释这个问题吗?

if you see this: 如果看到以下内容:

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

The range of long ie max is 9223372036854775807, 19 digits and once it reaches 10000000000000000000 (20 digits), it will try to convert this string to long which is out of range and hence you get an exception. long的范围(即max)为9223372036854754775807(19个数字),一旦达到10000000000000000000(20个数字),它将尝试将此字符串转换为超出范围的long,因此您将获得异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM