1
我希望能够读取其行和列的文本文件并将数据放入矩阵中。这是我到目前为止。我有一个类型为int的数据成员的矩阵类,它是一个二维数组[] []。扫描仪类和跳过模式
import java.io.*;
import java.util.*;
public class test{
public static void main(String args[]) throws FileNotFoundException {
File fin = new File ("matrix1.txt");
Scanner scanner = new Scanner(fin);
scanner.next(); // removes the first line in the input file
int rows = scanner.nextInt();
int cols = scanner.nextInt();
while (scanner.hasNextLine()){
String line = scanner.nextLine();
System.out.println(line);
}
System.out.println(rows);
System.out.println("/n");
System.out.println(cols);
}
}
示例文本文件如下。我想获取行和列,以便我可以动态声明矩阵并存储它的值。我得到错误说INPUTMISMATCH异常。帮助将不胜感激。
<matrix>
rows = 2
cols = 2
1 2
2 4
</matrix>
得到一个出界的错误,当我尝试这一点。 – dawnoflife 2011-02-15 09:23:28