在netbeans中,我在Person类中第55行的MyProj03类第35行的代码中得到了一个Array Index Out Of Bounds Exception错误。我不知道为什么我会收到这个错误。
我的代码:
import java.util.Scanner;
import java.io.*;
import birch.Person.*;
public class MyProj03 {
public static void main(String[] args) throws IOException {
// check for file existence
File file = new File("p3text.txt");
if (file.exists())
{
// read each record into a String
StringBuilder fileContents = new StringBuilder((int)file.length());
Scanner scanner = new Scanner("p3text.txt");
String lineSeparator = System.getProperty("line.separator");
try {
while(scanner.hasNextLine()) {
fileContents.append(scanner.nextLine() + lineSeparator);
Person one = new Person();
one.parseCommaDelim(fileContents.toString());
}
} finally
{
scanner.close();
}
}
else if (!file.exists())
{
System.out.println("The file p3text.txt is not found.");
System.exit(2);
}
}
}
更多代码:
public class Person {
//make instance fields for name, city, age, and SiblingCount
public String name;
public int age;
public String city;
public int sibCount;
public Person()
{
name = "";
age = 0;
city = "";
sibCount = 0;
}
// public access methods (getters)
public String getPerson() {
return this.name;
}
public int getAge() {
return this.age;
}
public String getCity() {
return this.city;
}
public int getSibCount() {
return this.sibCount;
}
// make a toString method
public String toString()
{
String str = "person: " + name + "age: " + age + "city: " + city;
return str;
}
// make a method called parseCommaDelim
public Person parseCommaDelim(String s) {
String[] tokens = s.split(",");
Person instance = new Person();
instance.name = tokens[0];
instance.age = Integer.parseInt(tokens[1]); //ArrayIndexOutOfBoundsException error
instance.city = tokens[2];
instance.sibCount = Integer.parseInt(tokens[3]);
return instance;
}
public int getIndex(Arrays list[], String key)
{
for (int index = 0; index< list.length; index++)
{
if ( list[index].equals(key) )
return index;
}
return -1;
}
}
我的文字文件
Rhonda, 20 , San Diego , 1
Kaitlin, 24 , Provo , 4
Bret, 24 , Columbia , 4
Chris, 28 , Escondido , 2
Dylan, 21, Portland, 3
答案 0 :(得分:0)
你应该替换
Person one = new Person();
one.parseCommaDelim(lineSeparator);
带
Person one = new Person();
one.parseCommaDelim(fileContents.toString());
因为您当前的实现尝试解析,
本身,而不是您读取的字符串。
答案 1 :(得分:0)
您可以逐行扫描文件的内容并使用以下代码进行处理:
public class MyProj03 {
public static void main(String[] args) throws IOException {
File file = new File("p3text.txt");
try {
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
Person one = new Person();
one.parseCommaDelim(line);
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
我还建议您更改片段代码:
Person instance = new Person();
instance.name = tokens[0];
instance.age = Integer.parseInt(tokens[1]); //ArrayIndexOutOfBoundsException error
instance.city = tokens[2];
instance.sibCount = Integer.parseInt(tokens[3]);
到此:
Person instance = new Person();
instance.name = tokens[0];
instance.age = Integer.parseInt(tokens[1].trim()); //ArrayIndexOutOfBoundsException error
instance.city = tokens[2];
instance.sibCount = Integer.parseInt(tokens[3].trim());