zpersi
zpersi

Reputation: 9

How to handle importing a CSV file with differing column lengths

Im working on a project for school and am having a really hard time figuring out how to import and format a CSV file into a usable format. The CSV contains a movie name in the first column, and showtimes in the rows, so it would look something like this.

movie1, 7pm, 8pm, 9pm, 10pm

movie2, 5pm, 8pm

movie3, 3pm, 7pm, 10pm

I think I want to split each row into its own array, maybe an arraylist of the arrays? I really dont know where to even start so any pointers would be appreciated.

Preferably dont want to use any external libraries.

Upvotes: 0

Views: 854

Answers (3)

M3SSYM4RV1N
M3SSYM4RV1N

Reputation: 135

You can use opencsv to read the CSV file and add each String[] to an ArrayList. There are examples in the FAQ section of opencsv's website.

Edit: If you don't want to use external libraries you can read the CSV using a BufferedReader and split the lines by commas.

    BufferedReader br = null;
    try{
        List<String[]> data = new ArrayList<String[]>();
        br = new BufferedReader(new FileReader(new File("csvfile")));
        String line;
        while((line = br.readLine()) != null){
            String[] lineData = line.split(",");
            data.add(lineData);
        }
    }catch(Exception e){
        e.printStackTrace();
    }finally{
        if(br != null) try{ br.close(); } catch(Exception e){}
    }

Upvotes: 0

Ted Cahall
Ted Cahall

Reputation: 196

I have no affiliation with Univocity - but their Java CSV parser is amazing and free. When I had a question, one of the developers got back to me immediately. http://www.univocity.com/pages/about-parsers

You read in a line and then cycle through the fields. Since you know the movie name is always there and at least one movie time, you can set it up any way you like including an arraylist of arraylists (so both are variable length arrays).

It works well with our without quotes around the fields (necessary when there are apostrophes or commas in the movie names). In the problem I solved, all rows had the same number of columns, but I did not know the number of columns before I parsed the file and each file often had a different number of columns and column names and it worked perfectly.

Upvotes: 0

Darshan Mehta
Darshan Mehta

Reputation: 30839

I would go with a Map having movie name as key and timings as list like the one below:

Map<String, List<String>> movieTimings = new HashMap<>();

It will read through csv file and put the values into this map. If the key already exists then we just need to add the value into the list. You can use computeIfAbsent method of Map (Java 8) to see whether the entry exists or not, e.g.:

public static void main(String[] args) {
    Map<String, List<String>> movieTimings = new HashMap<>();
    String timing = "7pm";//It will be read from csv
    movieTimings.computeIfAbsent("test", s -> new ArrayList<>()).add(timing);
    System.out.println(movieTimings);
}

This will populate your map once the file is read. As far as reading of file is concerned, you can use BuffferedReader or OpenCSV (if your project allows you to use third party libraries).

Upvotes: 0

Related Questions