Java BufferedReader while循环超出范围异常 [英] Java BufferedReader while loop out of bounds exception
本文介绍了Java BufferedReader while循环超出范围异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用BufferedReader从文本文件读取数据。变量 reg是我要访问的数据字符串中的第四个条目。
I am using BufferedReader to read data from a text file. The variable "reg" is the fourth entry in the string of data that I am trying to access.
我正在获取异常:线程 main中的异常java .lang.ArrayIndexOutOfBoundsException:3
I am getting the exception: "Exception in thread "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。
Your error has nothing to do with the reading. The error is that reservoir (sometimes) has a length less than 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循环超出范围异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文