java - 将 CSV 文件读入链接列表 (Java)
问题描述
所以我无法读取 CSV 文件并将其添加到我的链接列表中。我已经将 csv 文件读入了诸如数组、arraylists 之类的东西,但从来没有读过单链表。现在我的问题是我在解析薪水的代码行上不断收到 ArrayOutOfBoundsException。该代码是我的菜单类的一部分,我在其中读取文件并将其添加到链接列表中。所以我的问题是,如何正确地将 csv 文件读入链表?谢谢!
public class EmpMenu {
private SLList<Employee> list = new SLList<Employee>();
public EmpMenu() {
Scanner inFile = null;
try {
inFile = new Scanner(new File("employees.txt"));
} catch (FileNotFoundException x) {
System.err.print("File not found");
}
while (inFile.hasNextLine()) {
String line = inFile.nextLine();
String[] records = line.split(",[ ]");
String employeeID = records[0];
String fName = records[1];
String lName = records[2];
Double salary = Double.parseDouble(records[3]);
Employee employee = new Employee(employeeID, fName, lName, salary);
list.add(employee);
}
解决方案
您的问题似乎与此无关,LinkedList
并且ArrayOutOfBoundsException
表明该问题与拆分line
为records
字符串数组正确相关,records[3]
但不存在。所以检查它的长度,records
你会发现它有什么问题。
推荐阅读
- php - 将 Cloudinary 与 spatie/media-library Laravel 包一起使用
- node.js - Node/Express:使用 SuperAgent 从 URL 读取 JSON 文件
- python - Django - 模板渲染性能(我认为)如何检查启用 LocMemCache 是否有效?
- python - Python rsa 模块的麻烦
- business-intelligence - Cognos Analytics,交叉表中有多个列,但度量中只有一行
- django - Django,如何从 s3 迁移到 CDN?
- java - 子类测试中的 JUnit4 NullPointerException,覆盖方法
- javascript - 如何执行我的代码以获得想要的结果
- vue.js - Vuelidate 在点击操作时验证 bootstrapvue / vue 表单字段
- azure - 更新 Azure 机器人中的默认名称