gpt4 book ai didi

Java BufferedReader while循环越界异常

转载 作者:行者123 更新时间:2023-11-30 03:47:45 25 4
gpt4 key购买 nike

我正在使用 BufferedReader 从文本文件中读取数据。变量“reg”是我尝试访问的数据字符串中的第四个条目。

我收到异常:“线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: 3”

这是我的代码:

package calctest;

import static calctest.CalcTest.reg;
import java.io.*;

public class CalcTest {

static Integer reg, prov;

public static void main(String[] args) throws Exception{

String readFile = "M:\\MandNDrives\\mwallace\\JAVA for NEMS\\EORModule\\NEMSEORDB.txt";
BufferedReader br = null;
String line = "";
String delim = "[ ]+";

try {
br = new BufferedReader(new FileReader(readFile));
br.readLine();
while ((line = br.readLine()) != null) {

String [] reservoir = line.split(delim);

reg = Integer.parseInt(reservoir[3]);

System.out.println(reg);

}
}catch (FileNotFoundException e) {
}catch (IOException e) {

}

}

}

最佳答案

您的错误与阅读无关。错误是水库(有时)的长度小于 4。

 while ((line = br.readLine()) != null) {
String [] reservoir = line.split(delim);

for(String s : reservoir)
System.out.println(s); //Post what this outputs for debugging purposes

if (resivoir.length > 3)
reg = Integer.parseInt(reservoir[3]);
else
reg = ... //do something else...

System.out.println(reg);

}

关于Java BufferedReader while循环越界异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25191648/

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