0

I'm trying to convert a string into a Long/Int and keep getting a NumberFormatException. The string is a number i don't understand. I tried to Parse to a double and got infinity. What am i doing wrong?

public class EncryptDecrypt {
private char[] localKey;
private long keyNumber;
public EncryptDecrypt(char[] localKey) {
    this.localKey = localKey; // Brings in 256 random characters
    keyNumber = makeKeyNumber(localKey); 
    System.out.println(keyNumber); //prints long

}
private long makeKeyNumber(char[] key) {
    StringBuilder keyString = new StringBuilder();
    for(int i = 0; i<key.length; i++) {   //Builds String from chatacter array
        int ascii = (int) key[i];
        keyString.append(ascii);
    }
    String s = keyString.toString(); // Makes a string from stringbuilder
    long keyNum = Long.parseLong(s); // Attempts to convert string to long

    return keyNum; //returns long
}

}

Error

Exception in thread "main" java.lang.NumberFormatException: For input string: "1121214840113493593108124959933124991191111155510211549117935560351251051181216112311811354631064111712563371141044610510739101436457101108361109395411095555121465711133107466391125103119411219910436575950469549424363114114614597491064011312110398491254136371199942110115985141102122464410410060611086111311011210811242123981091054612045104911091191231131164345115124991245638913543123394658985539123112117113421225444351169157529911549435933949598434157591021241194249113425311535454957111108107611074064113944040601001145036333610510548933848994911550118593511910712163581141209743101643361100113999954931191246395107"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68)
at java.base/java.lang.Long.parseLong(Long.java:699)
at java.base/java.lang.Long.parseLong(Long.java:824)
at EncryptDecrypt.makeKeyNumber(EncryptDecrypt.java:18)
at EncryptDecrypt.<init>(EncryptDecrypt.java:7)
at Main.main(Main.java:15)
1
  • That number is too big for Long.parseLong. Use BigInteger instead. Commented Jan 19, 2020 at 14:15

1 Answer 1

1

1121214840113493593108124959933124991191111155510211549117935560351251051181216112311811354631064111712563371141044610510739101436457101108361109395411095555121465711133107466391125103119411219910436575950469549424363114114614597491064011312110398491254136371199942110115985141102122464410410060611086111311011210811242123981091054612045104911091191231131164345115124991245638913543123394658985539123112117113421225444351169157529911549435933949598434157591021241194249113425311535454957111108107611074064113944040601001145036333610510548933848994911550118593511910712163581141209743101643361100113999954931191246395107

Is too large to fit in any integer data type. Thus, I recommend BigInteger for this case.

Sign up to request clarification or add additional context in comments.

1 Comment

Alright thanks, I'll need to come up with a better solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.