我尝试将整数转换为数组,例如1234到 int[] arr = {1,2,3,4};
我写了一个函数
public static void convertInt2Array(int guess) {
String temp = Integer.toString(guess);
String temp2;
int temp3;
int [] newGuess = new int[temp.length()];
for(int i=0;i<=temp.length();i++) {
if (i!=temp.length()) {
temp2 = temp.substring(i, i+1);
} else {
temp2 = temp.substring(i);
//System.out.println(i);
}
temp3 = Integer.parseInt(temp2);
newGuess[i] = temp3;
}
for(int i=0;i<=newGuess.length;i++) {
System.out.println(newGuess[i]);
}
}
但是抛出一个异常:
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:504)
at java.lang.Integer.parseInt(Integer.java:527)
at q4.test.convertInt2Array(test.java:28)
at q4.test.main(test.java:14)
Java Result: 1
有什么想法吗?
慕斯王
Qyouu
皈依舞