我制作了一個(gè)小代碼塊,其目標(biāo)是嘗試將數(shù)字的所有數(shù)字存儲(chǔ)到數(shù)組中。例如,數(shù)字“123”將存儲(chǔ)為 {1,2,3}。一切似乎都運(yùn)行良好,除非數(shù)字的長(zhǎng)度大于10。是我的方法有問(wèn)題嗎?確切的錯(cuò)誤消息是線程“main” java.lang.NumberFormatException 中的異常:對(duì)于輸入字符串:“1202020202020202020” at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.base/java.lang.Integer.parseInt(Integer.java:652) at java.base/java.lang.Integer.parseInt(Integer.java:770) at test.main(test.java:8)public class test {public static void main(String[] args){ //This block of code parses the zipcode and stores each number it has into an array. It also fetches the length, which is used later. String input = args[0]; int length = input.length(); int zipcode = Integer.parseInt(args[0]); int[] digits = new int[length]; for(int i = 0; i < length ; i++){ digits[i] = zipcode % 10; zipcode = zipcode /10; }}}
1 回答

慕斯王
TA貢獻(xiàn)1864條經(jīng)驗(yàn) 獲得超2個(gè)贊
您的代碼將處理的最大數(shù)字是 Integer.MAX_VALUE,這是2147483647。除此之外,您正在嘗試解析一個(gè)不適合整數(shù)的數(shù)字。使用Long會(huì)給你更多的空間。
剛剛看到@user207421的評(píng)論,他/她是對(duì)的...你真的不需要把你的字符串存儲(chǔ)為一個(gè)數(shù)值。如果您必須這樣做,并且想要處理非常大的數(shù)字,則可以使用BigDecimal。
另外,根據(jù)你說(shuō)你想要的,我認(rèn)為你的最終數(shù)組將按照你想要的順序相反。
添加回答
舉報(bào)
0/150
提交
取消