gpt4 book ai didi

java.lang.ArrayIndexOutOfBoundsException : 5 WHILE LOOP

转载 作者:太空宇宙 更新时间:2023-11-04 06:12:17 25 4
gpt4 key购买 nike

我正在尝试在 while 循环中读取一个大的 csv 文件。但是当我运行代码时,我得到了 java.lang.ArrayIndexOutOfBoundsException: 5我是否缺少一个步骤?

  String csvFileToRead = "C:/automation/test.csv"; 

BufferedReader br = null;

String line = "";
String splitBy = ",";

try { boolean firstLine = true;
br = new BufferedReader(new FileReader(csvFileToRead));

while ((line = br.readLine()) != null ) {

if (firstLine) {
firstLine = false;
continue;}

String[] id = line.split(splitBy);
String replace = id[5].replace("\"", "");



String cardNo = id[5].replace("\"", "");


String fname = id[8].replace("\"", "");
String name = driver.findElement(By.id("bnxczxc")).getAttribute("value");


if (name.equals(fname)) {
//code
}
else {
//code
}


}

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();

}
catch (IOException e) {
e.printStackTrace();}}}

最佳答案

您最终会在 CSV 文件中遇到一行,其中至少没有 6 个值,因此该行会引发错误:

String replace = id[5].replace("\"", "");

我建议进行如下检查:

if(id.length > 0)
{
// Do the stuff you're trying to do
}
else
{
// Log or print error
}

如果您进一步排除故障,我希望您会发现您的程序正在尝试读取文件的最后一行,该行将为空。

关于java.lang.ArrayIndexOutOfBoundsException : 5 WHILE LOOP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28556596/

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