java - 为 csvReader 跳过了循环
问题描述
所以,我有这种情况:我有两个 .csv 文件,我需要为它们读取和保存数据。
问题在这里:
while ((nextPM = csvReader2.readNext()) != null) {
System.out.println(nextPM[0]);
while ((nextRecord = csvReader.readNext()) != null) {
System.out.println(nextRecord[0] + "asd");
if(nextRecord[0].equals(nextPM[0])) {
System.out.println(nextRecord[0] + " " + nextRecord[1] + " " + nextPM[2]);
}
}
}
第一次完美运行,但是当第一个 while 循环再次开始时,第二个 while 就被跳过了。有什么解决办法吗?当然nextPM
并且nextRecord
是String[]
(在我向您展示的代码之外初始化)
解决方案
当外部循环的第一次迭代完成内部循环时,cvsReader
将达到 EOF,因此任何进一步的调用csvReader.readNext()
都将返回 null。
一种解决方案是不嵌套循环,而是首先将其中一个文件读取到数组或其他集合中,然后在读取第二个数组时对该数组进行处理。
推荐阅读
- lua - 如何修复我在 Roblox Lua 中制作的脚本?
- android - 如何使用 2 个不相互独立的单独微调器更新 TextView?
- sql-server - 需要帮助让 Microsoft 本地网关与 Azure 分析服务一起使用
- html - 如何使悬停覆盖文本固定到图像
- laravel-5 - 为 Laravel 网站延迟 JS 脚本的最简单方法是什么?
- php - 使用 file_get_content php 获取搜索结果的内容
- c# - 字节溢出评估为零而不是异常?
- php - 如何从 PHP 7.2 降级到 PHP 5.6 EC2 linux
- android - 'android.disableResourceValidation=true' 是实验性的,在本机反应中不受支持
- c++ - 迭代器的引用参数错误的 C++ 函数。寻求解释