我正在编写一个带有csv文件的程序,并将每一行读入一个具有自己字段的USCrimeClass对象数组中。我想通过打印其中一个来查看阵列是否正确,但我得到的是OutOfBounds异常。
import java.io.*;
import java.util.*;
public class USCrimeClass
{
public int year;
public int population;
public int violentCrime;
public double violentCrimeRate;
public int manslaughter;
public double manslaughterRate;
public int rape;
public double rapeRate;
public int robbery;
public double robberyRate;
public int assault;
public double assaultRate;
public int propertyCrime;
public double propertyCrimeRate;
public int burglary;
public double burglaryRate;
public int larcenyTheft;
public double larcenyTheftRate;
public int vehicleTheft;
public double vehicleTheftRate;
public USCrimeClass(String line)
{
String[]split=line.split(",");
year=Integer.parseInt(split[0]);
population=Integer.parseInt(split[1]);
violentCrime=Integer.parseInt(split[2]);
violentCrimeRate=Double.parseDouble(split[3]);
manslaughter=Integer.parseInt(split[4]);
manslaughterRate=Double.parseDouble(split[5]);
rape=Integer.parseInt(split[6]);
rapeRate=Double.parseDouble(split[7]);
robbery=Integer.parseInt(split[8]);
robberyRate=Double.parseDouble(split[9]);
assault=Integer.parseInt(split[10]);
assaultRate=Double.parseDouble(split[11]);
propertyCrime=Integer.parseInt(split[12]);
propertyCrimeRate=Double.parseDouble(split[13]);
burglary=Integer.parseInt(split[14]);
burglaryRate=Double.parseDouble(split[15]);
larcenyTheft=Integer.parseInt(split[16]);
larcenyTheftRate=Double.parseDouble(split[17]);
vehicleTheft=Integer.parseInt(split[18]);
vehicleTheftRate=Double.parseDouble(split[19]);
}
public static void main(String[] args)
{
Scanner read = null;
{
try
{
read=new Scanner(new File("C:\\Crime.csv"));
}
catch(FileNotFoundException e)
{
System.out.println("The file can't be opened");
System.exit(0);
}
String[] lines = null;
read.nextLine();
while(read.hasNextLine())
{
lines=read.nextLine().split(",");
}
USCrimeClass[] CrimeYear=new USCrimeClass[lines.length];
for(int i=0;i<lines.length;i++)
{
CrimeYear[i]=new USCrimeClass(lines[i]);
}
read.close();
System.out.println(CrimeYear[0]);
}
}
}
这是csv文件应该是什么样子
1994,260327173,1130,3.7,3890,39.5,...
1995,250692382,2478,21.5,2230,28.6,...
1996,230492492,4092,22.8,3202,39.4...
这是错误消息
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at USCrimeClass.<init>(USCrimeClass.java:30)
at USCrimeClass.main(USCrimeClass.java:84)
答案 0 :(得分:1)
关于此代码:
while(read.hasNextLine())
{
lines = read.nextLine().split(",");
}
你在这里做的是逐行读取文件,但是你总是用当前读取的行覆盖lines
。最后lines
将只包含最后一行的拆分内容,而不包含csv文件中的所有行。
将每个读取行添加到列表中,然后这应该有效:
List<String> lines = new ArrayList<>();
read.nextLine();
while(read.hasNextLine())
{
lines.add(read.nextLine());
}
USCrimeClass[] CrimeYear = new USCrimeClass[lines.size()];
for(int i=0;i<lines.size();i++)
{
CrimeYear[i]=new USCrimeClass(lines.get(i));
}
read.close();
如果您不想缓冲所有读取行,您还可以直接创建相应的对象:
List<USCrimeClass> crimeClasses = new ArrayList<>();
read.nextLine();
while(read.hasNextLine())
{
crimeClasses.add(new USCrimeClass(read.nextLine()));
}
read.close();
System.out.println(crimeClasses.get(0));
BTW:我还建议将read.close()放入finally { ... }
块。