【发布时间】:2015-07-26 18:19:22
【问题描述】:
我是 Java(以及一般编程)的初学者。 我正在从包含以下文本的文本文件中输入信息:
戈登弗里曼 27
阿德里安·谢泼德 22
巴尼卡尔霍恩 19
爱丽克斯·万斯 23
我在这个方法中得到了一个 ArrayIndexOutOfBoundsException:
private static void readFile2() {
System.out.println("\nReading from file 2:\n");
File file = new File("C:/Users/Reflex FN/Documents/IOTest2/text.txt");
try {
BufferedReader readFromFile = new BufferedReader(
new FileReader(file));
String read = readFromFile.readLine();
while(read != null) {
String[] readSplit = read.split(" ");
int age = Integer.parseInt(readSplit[2]);
System.out.println(readSplit[0] + " is " + age + " years old.");
read = readFromFile.readLine();
}
readFromFile.close();
} catch (FileNotFoundException ex) {
System.out.println("File not found: " + ex.getMessage());
} catch (IOException ex) {
System.out.println("IO Exception: " + ex.getMessage());
}
}
这是第一次工作;它打印出来了:
戈登弗里曼今年 27 岁。
但是,在打印任何其他内容之前,已抛出 ArrayIndexOutOfBoundsException。 我到底做错了什么? 异常的来源似乎是这一行:
int age = Integer.parseInt(readSplit[2]);
顺便说一句,我是新来的,所以我希望我没有放错这个问题。
谢谢。 :)
【问题讨论】:
标签: java exception-handling text-files indexoutofboundsexception filereader