java - 将txt文件读入邻接矩阵
问题描述
我正在寻找一个包含这种格式的矩阵的文本文件:
1 2 3 4
1 3 1 4
1 5 5 1
1 1 1 1
dsadadasdasdasdasdasd
我想阅读文本文件直到空白行并忽略下面的其余文本。
我目前有这个,但不确定我是否接近。任何帮助,将不胜感激。谢谢
matrix = new int[4][4];
input = new Scanner(new BufferedReader(new FileReader("input.txt")));
String tempLine = null;
while ((tempLine = input.nextLine()) != null)
{
if(!tempLine.isEmpty())
{
//System.out.println(tempLine);
for(int i = 0; i < 4; i++)
{
String[] vals = tempLine.trim().split(" ");
for(int j=0; j < vals.length; j++)
{
matrix[i][j] = Integer.parseInt(vals[j]);
}
}
}
else
{
break;
}
}
input.close();
我当前得到的输出是一个 4 x 4 数组,其中包含每个条目的最后一行。
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
解决方案
推荐阅读
- node.js - 转发端口从 80 和 443 到 8080
- dynamic-programming - 从 1 到 n 的数字的集合位之和至少为 k
- node.js - 加载资源失败:服务器响应状态为 405(不允许)需要有关 nginx.conf 的帮助
- opencv - 需要帮助使用 open cv c++ 对对象进行姿态估计
- python - 否定一个正则表达式组
- c# - c#中的枚举数据类型是如何分配内存的
- php - 如何修复 Laravel 5.4 中的“尝试获取非对象的属性”错误
- node.js - Jasmine:测试一个方法是否被另一个类的另一个方法调用
- node.js - 动态使用 Alexa 演示语言
- jquery - 绑定数据到数据表时如何添加复选框?