gpt4 book ai didi

java - Parse Int/Parse long NumberFormatException 问题

转载 作者:行者123 更新时间:2023-12-02 09:05:58 25 4
gpt4 key购买 nike

我正在尝试将字符串转换为 Long/Int 并不断收到 NumberFormatException。该字符串是一个我不明白的数字。我尝试解析为 double 并得到无穷大。我做错了什么?

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
}

}

错误

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)

最佳答案

11212148401134935931081249599331249911911111555102115491179355603512510511812161123118113546310641117125633711410446 105107391014364571011083611093954110955551214657111331074663911251031194112199104365759504695494243631141146145974910640113 121103984912541363711999421101159851411021224644104100606110861113110112108112421239810910546120451049110911912311311643451 151249912456389135431233946589855391231121171134212254443511691575299115494359339495984341575910212411942491134253115354549 571111081076110740641139440406010011450363336105105489338489949115501185935119107121635811412097431016433611001139999549311 91246395107

太大,无法容纳任何整数数据类型。因此,对于这种情况,我推荐 BigInteger

关于java - Parse Int/Parse long NumberFormatException 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59810845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com