情况:
我正在尝试使用parseInt
将字符串转换为整数,这是我的代码:
public static int calcIMEI(String imei) {
int[] importance = {1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2};
int[] imeiCode = new int[imei.length()];
int calcBase, crossSum;
String iMeiNumber = "";
// Turns chars into integer and
for (int i = 0; i < imei.length(); i++) {
imeiCode[i] = imei.charAt(i) - 48;
}
// Adds pre-defined importance to the values in the array
for (int i = 0; i<imeiCode.length; i++) {
imeiCode[i] = (imeiCode[i] * importance[i]);
}
// Turns integer array into one String
for (int i = 0; i<imeiCode.length; i++) {
iMeiNumber += Integer.toString(imeiCode[i]);
}
// Parses string into one single integer
calcBase = Integer.parseInt(iMeiNumber); // error occurs here (!)
crossSum = crossSum(calcBase);
return crossSum;
}
static int crossSum(int zahl) {
int qSum = 0;
while(zahl > 0) {
qSum = qSum + (zahl % 10);
zahl = zahl / 10;
}
return qSum;
}
public static void main(String[] args) {
System.out.println(calcIMEI("44780651751725"));
}
问题:
以下错误消息属于标记的行:
线程“ main”中的异常java.lang.NumberFormatException:
对于输入字符串:“ 5256556448605350555849625058”
在java.lang.NumberFormatException.forInputString(未知来源)
在java.lang.Integer.parseInt(未知来源)
在java.lang.Integer.parseInt(未知来源)
在Pruefziffern.calcIMEI(Pruefziffern.java:23)
在Pruefziffern.main(Pruefziffern.java:41)
问题:
为什么会出现此错误?