简体   繁体   English

为什么这种转换会产生错误?

[英]Why does this conversion give an error?

I have this code in eclipse: 我在eclipse中有以下代码:

        String A = String.valueOf(a);
        String B = String.valueOf(b);
        String C = String.valueOf(c);
        String D = String.valueOf(d);
        String E = String.valueOf(e);
        String F = String.valueOf(f);
        String G = String.valueOf(g);
        String H = String.valueOf(h);
        String I = String.valueOf(i);
        String J = String.valueOf(j);
        String K = String.valueOf(k);

        String rawpassword = A+B+C+D+E+F+G+H+I+J+K;
        int password = Integer.parseInt(rawpassword);
        System.out.println(password);

And it gives me this error 这给了我这个错误

at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:495)
    at java.lang.Integer.parseInt(Integer.java:527)
    at com.jakibah.codegenerator.Main.Generate(Main.java:65)
    at com.jakibah.codegenerator.Main.run(Main.java:24)
    at java.lang.Thread.run(Thread.java:745)

But I do not understand why. 但是我不明白为什么。 can someone help me? 有人能帮我吗?

    String A = String.valueOf(10);
    String B = String.valueOf(10);
    String C = String.valueOf(10);
    String D = String.valueOf(10);
    String E = String.valueOf(10);
    String F = String.valueOf(10);
    String G = String.valueOf(10);
    String H = String.valueOf(10);
    String I = String.valueOf(10);
    String J = String.valueOf(10);
    String K = String.valueOf(10);


    String codestring = A+B+C+D+E+F+G+H+I+J+K;
    BigInteger bigInteger = new BigInteger(codestring);
    System.out.println(bigInteger.max(bigInteger));

The parseInt(String s) method throws a NumberFormatException if the argument is not a parseable Integer . 如果参数不是可解析的IntegerparseInt(String s)方法将引发NumberFormatException

Make sure the String you pass to the method is a Number and is between -2^31 and 2^31 - 1 确保您传递给该方法的String是一个Number ,并且介于-2^312^31 - 1之间

Number Format exception arise when you trying to convert a string type into a integer but that does not fit as a integer. 当您尝试将字符串类型转换为整数但不适合整数时,会出现“数字格式”异常。 From your code I am not able to understand what value is a,b,c,d,.. From my experience I have uploaded two image to show you may be this kind of error you done 从您的代码中,我无法理解a,b,c,d ,.是什么值。根据我的经验,我上传了两张图片以显示您可能是这种错误

在此处输入图片说明

here number format exception arise as codestring is 10.320 and it is a string type so when compiler try to convert it as a string it unable to convert it due to . 此处出现数字格式异常,因为代码字符串为10.320,并且它是字符串类型,因此当编译器尝试将其转换为字符串时,由于不能转换。

在此处输入图片说明

But in this scenario codestring is 1020 , so easily it convert to a int. 但是在这种情况下,代码字符串为1020,因此很容易将其转换为int。

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

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