我無法找到我的程序中的任何問題。每次用戶輸入一個數字時,我都希望它將它保存在陣列A上,但是當用戶嘗試鍵入第二個數字時,會出現NumberFormatException
錯誤。異常在線程「主」java.lang.NumberFormatException:對於輸入字符串:「」陣列
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:470)
at java.lang.Integer.parseInt(Integer.java:499)
at practice.test(end.java:22)
at end.main(end.java:7)
下面是程序:
import java.io.*;
class end {
public static void main(String[] args) throws IOException {
practice obj = new practice();
obj.test();
}
}
class practice {
void test() throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
int A[] = new int[5];
String x;
int a, b, c, i = 0;
for(i = 0; i < 5; i++) {
System.out.println("Insert a number");
x = br.readLine();
A[i] = Integer.parseInt(x);
}
}
}
有無你試過打印出'x'是什麼? – APerson 2014-11-21 03:42:47
對我來說可行... – MadProgrammer 2014-11-21 03:45:59
這條線很有意義對於輸入字符串:「」你不能將空字符串轉換爲整數 – 2014-11-21 03:46:30