在文件 I/O 期间获取 IndexOutOfBoundsException
Getting IndexOutOfBoundsException during File I/O
我正在做一项作业,我必须读取一个包含美国 States/territories 及其人口名称的 .txt 文件,并将状态分配给一个 ArrayList,将人口分配给另一个(并行) .
.txt文件格式如下
state name,123456
state name,123456
所以名称和整数信息之间的分隔是“,”字符。
我写了下面的方法来读取文件:
public static void readFile(ArrayList<String>states, ArrayList<Integer> pops){
try{
Scanner read = new Scanner(new File("StatesPop.txt"));
while(read.hasNext()){ //while there is still content to read
if(read.hasNextInt()){ //if the next info is an int
pops.add(read.nextInt()); //read it and add it to the pop list
if(read.hasNextLine()){ //if there is a next line
read.nextLine(); //moves to the beginning of the next line
}
}
else{ //if it's not an int, it's a char
read.useDelimiter(","); //read until hitting a comma
String name = read.next();
states.add(name); //add to the states names list
}
}
read.close();
}
catch(FileNotFoundException fnf){
System.out.println("File was not found.");
}
}
我得到的报错方法如下:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 1 out of bounds for length 1
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
at java.base/java.util.Objects.checkIndex(Objects.java:372)
at java.base/java.util.ArrayList.get(ArrayList.java:459)
at Main.sortStates(Main.java:75)
at Main.menu(Main.java:174)
at Main.main(Main.java:15)
public static void readFile(ArrayList<String> states, ArrayList<Integer> pops) {
try {
Scanner read = new Scanner(new File("StatesPop.txt"));
while (read.hasNextLine()) {
String line = read.nextLine();
String[] lineSplit = line.split(",");
states.add(lineSplit[0]);
pops.add(Integer.valueOf(lineSplit[1]));
}
read.close();
} catch (FileNotFoundException fnf) {
System.out.println("File was not found.");
}
}
//[state name, state name]
//[123456, 123456]
我正在做一项作业,我必须读取一个包含美国 States/territories 及其人口名称的 .txt 文件,并将状态分配给一个 ArrayList,将人口分配给另一个(并行) . .txt文件格式如下
state name,123456
state name,123456
所以名称和整数信息之间的分隔是“,”字符。 我写了下面的方法来读取文件:
public static void readFile(ArrayList<String>states, ArrayList<Integer> pops){
try{
Scanner read = new Scanner(new File("StatesPop.txt"));
while(read.hasNext()){ //while there is still content to read
if(read.hasNextInt()){ //if the next info is an int
pops.add(read.nextInt()); //read it and add it to the pop list
if(read.hasNextLine()){ //if there is a next line
read.nextLine(); //moves to the beginning of the next line
}
}
else{ //if it's not an int, it's a char
read.useDelimiter(","); //read until hitting a comma
String name = read.next();
states.add(name); //add to the states names list
}
}
read.close();
}
catch(FileNotFoundException fnf){
System.out.println("File was not found.");
}
}
我得到的报错方法如下:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 1 out of bounds for length 1
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
at java.base/java.util.Objects.checkIndex(Objects.java:372)
at java.base/java.util.ArrayList.get(ArrayList.java:459)
at Main.sortStates(Main.java:75)
at Main.menu(Main.java:174)
at Main.main(Main.java:15)
public static void readFile(ArrayList<String> states, ArrayList<Integer> pops) {
try {
Scanner read = new Scanner(new File("StatesPop.txt"));
while (read.hasNextLine()) {
String line = read.nextLine();
String[] lineSplit = line.split(",");
states.add(lineSplit[0]);
pops.add(Integer.valueOf(lineSplit[1]));
}
read.close();
} catch (FileNotFoundException fnf) {
System.out.println("File was not found.");
}
}
//[state name, state name]
//[123456, 123456]