gpt4 book ai didi

java - 如何修复 java.lang.ArrayIndexOutOfBoundsException 错误

转载 作者:行者123 更新时间:2023-12-01 23:01:52 24 4
gpt4 key购买 nike

尝试使用 JFileChooser 扫描文本文件将其添加到数组并将其中一个字符串解析为 double 型并将两个字符串解析为整数时遇到错误。这是否与 addEmployee 方法将六个参数添加到 arrayList 有关?这是代码...

else if (e.getSource()==readButton){
JFileChooser fileChooser = new JFileChooser("src");
if (fileChooser.showOpenDialog(null)==JFileChooser.APPROVE_OPTION)
{
empFile=fileChooser.getSelectedFile();
}
Scanner scan = new Scanner("empFile");
while(scan.hasNext()){
String[] rowData = scan.next().split(":");
if(rowData.length == 5){
rowData[4] = null;
fName = rowData[0];
lName = rowData[1];
position2 = rowData[2];
firstParam = Double.parseDouble(rowData[3]);
secondParam = Integer.parseInt(rowData[4]);
empNum = Integer.parseInt(rowData[5]);
}
else{
fName = rowData[0];
lName = rowData[1];
position2 = rowData[2];
firstParam = Double.parseDouble(rowData[3]);
secondParam = Integer.parseInt(rowData[4]);
empNum = Integer.parseInt(rowData[5]);
}
if (position2.equals("Manager")){
c.addEmployee(fName, lName, position2, firstParam, 0, empNum);
}
else if(position2.equals("Sales")){
c.addEmployee(fName, lName, position2, firstParam, 0, empNum);
}
else{
c.addEmployee(fName, lName, position2, firstParam, secondParam, empNum);
}
}

}

John:Smith:Manufacturing:6.75:120:444

Betty:White:Manager:1200.00:111

Stan:Slimy:Sales:10000.00:332

Betty:Boop:Design:12.50:50:244

最佳答案

您正在尝试获取 empNum = Integer.parseInt(rowData[5]);

行数据只有大小 5,这意味着索引 0-4,这就是 ArrayIndexOutOfBoundsException 出现的原因

因此初始化 String[] rowData = new String[6];

关于java - 如何修复 java.lang.ArrayIndexOutOfBoundsException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23305252/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com