【发布时间】:2019-03-20 14:54:20
【问题描述】:
我需要从 txt 文件中读取图形矩阵。例如
4
0 1 1 0
1 0 1 0
1 1 0 1
0 0 1 0
这是输入文件。第一个数字是顶点数。我试过这样的事情:
Scanner sc = new Scanner(file.getAbsolutePath());
int n = sc.nextInt();
int [][] graph = new int [n][n];
for (int x =0; x<n; x++)
for (int y=0; y<n;y++)
graph[x][y] = sc.nextInt();
但我收到了InputMismatchException。我知道这意味着什么,但我不明白有什么问题。文件包含int 类型字符,我正在使用nextInt();有什么建议么?也许更简单的方法将矩阵转换为数组?感谢您的建议。
【问题讨论】: