java - 为什么我的程序打印值没有相应的打印语句?
问题描述
我试图弄清楚如何从java中的文件中读取一系列值。该文件有多行,每行中的值用逗号分隔。在编写测试程序只是为了弄清楚如何在 Scanner 中使用分隔符时,我遇到了我的程序从文件中打印值的问题。我不知道程序从哪里获取打印所有值的指令。
这就是我的 public static void main 中的内容(在 try 循环中):
File f1 = new File("Data1.txt");
File test = new File("test.txt");
Scanner reader = new Scanner(f1);
Scanner testReader = new Scanner(test);
testReader.useDelimiter(",");
System.out.println("line 18 "+testReader.nextInt());
System.out.println("line 19 "+testReader.nextInt());
System.out.println("line 20 "+testReader.next());
System.out.println("line 21 "+testReader.nextInt());
我正在读取的文件是 test.txt:
4,5,6
7
8,9,10
这就是正在打印的内容:
line 18 4
line 19 5
line 20 6
7
8
line 21 9
解决方案
您还需要将换行符添加到分隔符模式中:
testReader.useDelimiter(",|(\r\n)|(\n)");
推荐阅读
- windows-10 - WSL 2 上的 Ubuntu 18.04 LTS 仅在第一次 ping 时解析 DNS
- git - 如何从子分支中删除父提交
- c++ - 一种值类型的容器迭代器模板
- c# - 如何通过代码直接使用 EF Core Migrations API
- r - 使用 by_cases() 时,R 中的 Huxtable 格式错误
- typescript - 类的属性作为类并访问父属性
- bash - 根据日期解析 access.log
- algorithm - 比较两个不同字典中的值
- javascript - 如何将ajax代码转换为django中的vue.js axios方法
- python - Pandas/Matplotlib:如何更改 x 轴的比例