不久前我发布了一个问题,这个问题得到了回答,并且帮了我很大的忙。很抱歉,请尽快发布。 =/数组的结果为空?
我已经过了这个,不知道我做错了什么,打印的结果是几个'空'字。我正在尝试使用方法从文本文件中填充数据。然后在主类中运行该方法以打印出已填充的数组。
任何帮助将不胜感激,谢谢你提前。 =)
主类:
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文件是怎么样的? –
我认为你的内循环不会在最后一个逗号之后读取条目*。这可能是一个问题吗? – Thilo
用数据编辑。 – user1719605