我必須從這樣的文件中讀取數據:從文件讀入2D陣列
4
192 48 206 37 56
123 35 321 21 41
251 42 442 32 33
的第一數目的候選的總數(列)和我需要存儲用於其它使用該值。然後我需要將剩餘的數據讀入二維數組中。我用我現在的版本更新了我的代碼,但它仍然無法工作。我不斷收到錯誤 java.util.NoSuchElementException:沒有找到行
public static int readData(int[][] table, Scanner in)throws IOException
{
System.out.println("Please enter the file name: ");
String location = in.next();
Scanner fin = new Scanner(new FileReader(location));
int candidates = fin.nextInt();
fin.nextLine();
for (int row = 0; row < 5; row++) {
for (int column = 0; column < candidates; column++) {
String line = fin.nextLine();
fin.nextLine();
String[] tokens = line.split(" ");
String token = tokens[column];
table[row][column] = Integer.parseInt(token);
}
}
fin.close();
return candidates;
}
}
定義「完全不工作」。發生什麼事?華夫餅是否從屏幕上出來?首先,你忽略了「候選人」。其次,您只需閱讀第一行:'fin.nextLine();'應該在每次迭代中。 – Tunaki
獲取錯誤 java.lang.NumberFormatException:對於輸入字符串:「」我輸入文件名後 – Zackary
我如何忽略候選人?我不明白。 – Zackary