【发布时间】:2016-03-02 20:55:36
【问题描述】:
我必须从这样的文件中读取数据:
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;
}
}
【问题讨论】:
-
定义“完全不工作”。怎么了?华夫饼会从屏幕上出来吗?首先,您忽略了
candidates。其次,您只是在阅读第一行:fin.nextLine();应该在每次迭代中。 -
收到错误 java.lang.NumberFormatException: For input string: "" after I enter the filename
-
我怎么会忽略候选人?我不明白。
-
您在循环中硬编码 5,而不是解析
candidates,这是要读取的预期行数。然后,您不会在每次迭代中阅读。 -
啊,我明白了,谢谢。