【发布时间】:2020-10-20 21:43:49
【问题描述】:
public static Matrix read(String filename) {
Scanner scan = new Scanner(filename);
int row = scan.nextInt();
int column = scan.nextInt();
Matrix mat = new Matrix(row, column);
scan.nextLine();
for(int i=0; i<row; i++) {
for(int j=0; j<column; j++) {
mat.setElement(i, j, scan.nextInt());
}
scan.nextLine();
}
scan.close();
return mat;
}
这是我目前读取文本文件的方法。每个文本文件的顶部都有矩阵尺寸,然后是下面的矩阵行。例如:
2 3
1 1 1
2 2 2
当我尝试将第一行中的前 2 个数字存储为行和列索引时,出现输入不匹配异常。
【问题讨论】: