我一直在尋找這個答案,而且我還沒有找到一個已經糾正這個問題的人。我試圖填充數組,但檢查它是否大於某個值。然而,當我運行這個,我得到一個異常在線程「主要」java.lang.NumberFormatException:對於輸入字符串:「」錯誤。下面是代碼它:String到Int的java.lang.NumberFormatException Java
public void setReadData() throws NoSuchElementException {
try {
for (int i = 0; i < getRows(); i++) {
String numbers = p.nextLine();
String[] inputs = numbers.split(" ");
if (inputs.length > getCols()) {
System.out.println("There are more columns than the size specified!\n");
System.exit(1);
}
for (int j = 0; j < getCols(); j++) {
int data = Integer.parseInt(inputs[j]);
this.setData(i,j,data);
}
}
和錯誤代碼是:
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 foo.Bar.setReadData(Matrix.java:49)
at foo.Bar.main(Matrix.java:226)
任何幫助是非常感謝!
---- ----編輯
爲了澄清,該工作的舊代碼爲:
for (int i = 0; i < getRows(); i++) {
p.nextLine();
for (int j = 0; j < getCols(); j++) {
this.setData(i,j,p.nextInt());
}
}
然而,沒有檢查整條生產線已被檢查。
顯然,'inputs [j]'是''「'。 –
閱讀http://code.google.com/p/guava-libraries/wiki/StringsExplained –
中有關Splitter的部分空輸入字符串不是有效的整數值。所以你可能會以空字符串結束。你確定你的數據是你相信的嗎? –