나는 csv 파일을 사용하고 각 행을 자체 필드가있는 USCrimeClass 객체의 배열로 읽는 프로그램을 작성하고 있습니다. 나는 그 중 하나를 인쇄하여 배열이 올바르게 만들어 졌는지보고 싶지만 OutOfBounds 예외가 발생합니다. 여기 배열로 CSV 파일을 읽은 후 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)
이 예외는 어디에서 발생합니까? – UnholySheep
Crime.csv 파일의 모양과 오류의 예를 들어 줄 수 있습니까? 나는 파일이 어떻게 보일지 추측하고 디버깅 할 예제를 만들려고 노력했지만, 확실히 알기는 어렵다. – BrainFRZ
오류는 30 번과 84 번 줄에서 발생합니다. 배열이 만들어지면 건너 뛴 각 범주의 머리글이 표시되고 각 행마다 년, 인구 및 다중 범죄가 발생합니다 활동 및 발생 빈도 – Matt