简体   繁体   中英

Why does this conversion give an error?

I have this code in 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 .

Make sure the String you pass to the method is a Number and is between -2^31 and 2^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

在此处输入图片说明

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 .

在此处输入图片说明

But in this scenario codestring is 1020 , so easily it convert to a int.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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