Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
342 views
in Technique[技术] by (71.8m points)

Trying read csv file into java, set each filed as object and save into javalist, but missing a value in the csv file

i m trying read csv data to java, and set each fields as object and add into the arraylist, but i got a different csv file which is missing one vaules, the error is java.lang.ArrayIndexOutOfBoundsException: 6.

CSV files

public void loadAircraftData(Path p) throws DataLoadingException {  
    try {
        //open the file
        BufferedReader reader = Files.newBufferedReader(p);
        
        //read the file line by line
        String line = "";
        
        //skip the first line of the file - headers
        reader.readLine();
        
        
        while( (line = reader.readLine()) != null) {
            //each line has fields separated by commas, split into an array of fields
            String[] fields = line.split(",");
            
            
            //put some of the fields into variables: check which fields are where atop the CSV file itself
            String tailcode = fields[0];
            String model = fields[1];
            String type = fields[2];
            //Manufacturer manufacturer = fields[3];
            String staringPoistion = fields[4];
            int seats = Integer.parseInt(fields[5]);
            int cabincrewrequired= Integer.parseInt(fields[6]);
            
            
            //create an Aircraft object, and set (some of) its properties
            Aircraft a = new Aircraft();
            a.setTailCode(tailcode);
            a.setModel(model);
            a.setTypeCode(type);
            //a.setManufacturer(Manufacturer);
            a.setStartingPosition(staringPoistion);;
            a.setSeats(seats);
            a.setCabinCrewRequired(cabincrewrequired);
            a.setManufacturer(Manufacturer.AIRBUS);
            a.setManufacturer(Manufacturer.FOKKER);
            a.setManufacturer(Manufacturer.EMBRAER);
            
            
            //add the aircraft to our list
            aircraft.add(a);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I have an alternative solution that is different that use a loop. My idea is to have a method that checks if the index exists and return the value of this index, if not exist I can provide a default value.

Class

public class Main {

    public static void main(String[] args) {
        String[] fields = "2,2,2,2,2,2,".split(",");

        int seats = Integer.parseInt(getValue(fields, 5, "0"));
        int cabincrewrequired = Integer.parseInt(getValue(fields, 6, "0"));

        System.out.println(seats);
        System.out.println(cabincrewrequired);
    }

    private static String getValue(String[] fields, int index, String defaultValue) {
        return fields.length > index ? fields[index] : defaultValue;
    }
}

Output

2
0

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...