【发布时间】:2012-10-04 11:27:14
【问题描述】:
我不久前发布了一个问题,该问题得到了回答并帮助了我很多。抱歉这么快又发帖了。 =/
我已经检查过了,不确定我做错了什么,打印的结果是几个“空”字。我正在尝试使用一种方法用文本文件中的数据填充二维数组。然后在主类中运行该方法以打印出填充的数组。
任何帮助将不胜感激,在此先感谢您。 =)
主类:
public static void main(String[] args) throws IOException {
ScoreProcessor scores = new ScoreProcessor();
String[][] table = scores.readFile();
for (int row = 0; row < table.length; row++) {
for (int col = 0; col < table[row].length; col++) {
System.out.print(table[row][col] + "\t");
}
}
}
另一个类,包含方法:
public class ScoreProcessor {
static public String[][] readFile() throws IOException {
File filedata = new File("src/JavaApp2/Data.txt");
Scanner file = new Scanner(filedata);
int row = 0, col = 0;
String[][] scores = new String[8][5];
while (file.hasNext()) {
Scanner readfile = new Scanner(filedata);
readfile.nextLine();
readfile.useDelimiter(",");
while (readfile.hasNext(",")) {
String line = readfile.next();
line = scores[row][col] ;
col++;
} // end innerloop
row++;
col = 0;
} // end outerloop
return scores;
} // end method
} // end class
数据文件
Student,Q1,Q2,Q3,Q4
abcd0001,50,55,59,72
efgh0002,82,78,84,86
ijkl0003,42,45,46,52
mnop0004,66,74,72,78
qrst0005,82,86,89,88
uvwx0006,77,83,87,82
yzab0007,62,64,70,68
【问题讨论】:
-
你的 Data.txt 文件是什么样子的?
-
我认为,您的内部循环不会读取 after 最后一个逗号的条目。会不会是个问题?
-
请也向我们展示输出。是每个元素
null还是只有一个字段? -
为什么要开2个扫描仪???