Java:将csv文件读入数组错误

时间:2016-12-17 20:55:40

标签: java csv

所以我有一个从csv文件中获取数据的项目。我已经将每列传递给了一个字符串数组。当我尝试获取该数组的内容并将数据放入自己的数组时,它会传递错误,因为正在读取列标题并且我无法编辑该文件(项目的req)。

Crime.java

package crimestat;

import java.io.*;
import java.util.*;

public class Crime {

    private String Label[];
    private int Year[];
    private int Population[];
    private int Violent[];
    private double Violent_Rate[];
    private int Murder[];
    private double Murder_Rate[];
    private int Rape[];
    private double Rape_Rate[];
    private int Robery[];
    private double Robery_Rate[];
    private int Assault[];
    private double Assault_Rate[];
    private int Property[];
    private double Property_Rate[];
    private int Burglary[];
    private double Burglary_Rate[];
    private int Larceny[];
    private double Larceny_Rate[];
    private int Vehicle[];
    private double Vehicle_Rate[];

    public Crime() {
        Label = null;
        Year = null;
        Population = null;
        Violent = null;
        Violent_Rate = null;
        Murder = null;
        Murder_Rate = null;
        Rape = null;
        Rape_Rate = null;
        Robery = null;
        Robery_Rate = null;
        Assault = null;
        Assault_Rate = null;
        Property = null;
        Property_Rate = null;
        Burglary = null;
        Burglary_Rate = null;
        Larceny = null;
        Larceny_Rate = null;
        Vehicle = null;
        Vehicle_Rate = null;
    }

    public String get_Label(int i){
        return Label[i];
    }

    public int get_Year(int i){
        return Year[i];
    }

    public void set_Label(String[] Label){
        this.Label = Label;
    }

    public void set_Year(int i, Crime crime){
        this.Year[i] = Integer.parseInt(crime.get_Label(0));
    }

}

testCrime.java

package crimestat;

import java.io.*;
import java.util.*;

public class testCrime {

    static Crime obj = new Crime();

    public static void main(String[] args) throws IOException {
        Scanner file = null;
        int n=0;
        try {
            file = new Scanner(new BufferedReader(new FileReader("Crime.csv")));
            while (file.hasNextLine()) {
                obj.set_Label(file.nextLine().split(","));
                obj.set_Year(n,obj); 
                System.out.println(obj.get_Year(n));
            }
        }
        catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

错误

Exception in thread "main" java.lang.NumberFormatException: For input string: "Year"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at crimestat.Crime.set_Year(Crime.java:69)
at crimestat.testCrime.main(testCrime.java:20)

enter image description here

1 个答案:

答案 0 :(得分:0)

没有必要编辑文件。 由于第一列 title 。您可以存储第二列的数据。

代码如下:

    file = new Scanner(new BufferedReader(new FileReader("Crime.csv"))); 

    while(file.hasNextLine()){
        String curLine = file.nextLine();
        obj.set_Label(curLine.substring(curLine.indexOf(",")+1).split(","));//filter the first column
        obj.set_Year(n,obj); 

        System.out.println(obj.get_Year(n));

    }
}
catch(IOException ex){
    ex.printStackTrace();
}

 //update set_Label method
 public void set_Label(String[] Label){
     this.Label = new int[Label.length];
     for(int i = 0; i < Label.length;i++){
         this.Label[i] = Integer.valueOf(Label[i]);
     }
 }