Java 为什么在分析行时会出现数组索引越界异常错误?
在netbeans中,我在MyProj03类的第35行代码中从Person类的第55行代码中得到了一个数组索引越界异常错误。我不知道为什么会出现这个错误 我的代码:Java 为什么在分析行时会出现数组索引越界异常错误?,java,arrays,indexoutofboundsexception,Java,Arrays,Indexoutofboundsexception,在netbeans中,我在MyProj03类的第35行代码中从Person类的第55行代码中得到了一个数组索引越界异常错误。我不知道为什么会出现这个错误 我的代码: import java.util.Scanner; import java.io.*; import birch.Person.*; public class MyProj03 { public static void main(String[] args) throws IOException {
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;
}
你应该替换
Person one = new Person();
one.parseCommaDelim(lineSeparator);
与
当前实现试图解析
,
本身,而不是读取的字符串。您可以逐行扫描文件的内容,并使用此代码进行处理:
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());
tokens
的大小是多少?请发布p3text.txt
的内容,您希望lineSeparator
保存什么???如果1
已经超出范围,则split
命令不会返回4长度的数组。请检查您的字符串。考虑检查字符串以防止这些错误的机制。令牌是默认长度。我认为这样的代码应该在循环中。。。看起来每一行都是一个人的记录。@SJuan76,是的,这很有道理。谢谢。这救了我。
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());