gpt4 book ai didi

java - 如何消除索引越界异常的错误

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

您好,我在 if(numList.get(x) == ""&& numList.get(x+1) == "") 行中不断收到 java.lang.IndexOutOfBoundsException。我该如何解决这个问题?

BufferedReader br = null;
String row;
String[] data = null;
List<String> numList = new ArrayList<String>();

BufferedReader csvReader = new BufferedReader(new FileReader("input file lab5.csv"));
while ((row = csvReader.readLine()) != null) {
data = row.split(",");
// do something with the data

for(int x=0; x<data.length; x++) {
numList.add(data[x]);
}
numList.add(" ");
}

System.out.println("Make sure excel file is on src.\nNumbers extracted from file: "+numList);
int n=0;

for(int x=0; x<numList.size(); x++) {
if(numList.get(x) == " " && numList.get(x+1) == " ") {
n = x+1;
break;

最佳答案

为最后一个索引处理添加一个检查 if 子句 (x < numList.size()-1)

if (numList.get(x) == " " && x < numList.size()-1 && numList.get(x+1) == " ") {

关于java - 如何消除索引越界异常的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58444705/

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