Jakibah
Jakibah

Reputation: 53

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?

Upvotes: 0

Views: 76

Answers (3)

bhanu avinash
bhanu avinash

Reputation: 482

    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));

Upvotes: 3

Abhijit Chowdhury
Abhijit Chowdhury

Reputation: 222

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

enter image description here

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 .

enter image description here

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

Upvotes: 0

thegauravmahawar
thegauravmahawar

Reputation: 2823

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

Upvotes: 1

Related Questions