Parsing csv data into object with different column length

Woden :

I am new to Java and practicing parsing csv file into the object. I've tried but cannot figure it out.

The file looks like this:

  [0],    [1], [2],    [3]  ,    [4]    ,   [5]   ,  [6] ,   [7]  ,  [8] , [9]
class, gender, age, bodyType, profession, pregnant, isYou ,species, isPet, role
scenario:green,   ,         ,           ,         ,        ,      ,      ,
person, female, 24, average ,           , FALSE   ,        ,      ,      , passenger
animal, male  ,  4,         ,           , FALSE   ,        , dog  , TRUE , pedestrian
scenario:red
person, male  , 16, athletic, boxer     , FALSE   ,  TRUE  ,      ,      , passenger
person, female, 25, athletic, doctor    , TRUE    ,  FALSE ,      ,      , pedestrian

I need to parse it by any number of passengers and pedestrians with any scenarios. Finally, add these scenarios into an ArrayList for analyzing.

What I think is to:

  1. loop through each line, stops when reaches to the next scenario:red, adds the passengers and the pedestrians to the Character ArrayList. (I've done adding, but don't how to stop).
  2. Create a scenario using constructor scenario(ArrayList<Character> passenger, ArrayList<Character> pedestrians, boolean redOrGreen);
  3. The ArrayList scenarios add the created scenarios.

What I've done is put everything together instead of separate them. Any help or hint is highly appreciated.

Thanks for this community who helped me, here is what I've got so far.

    public void loadCsv() throws IOException {

    String csvFile = "config.csv";
    String line = "";
    String csvSplit = "\\s*,\\s*";

    Scenario scenario = new Scenario();
    Person person = new Person();
    Animal animal = new Animal();

    ArrayList<Scenario> scenaios = new ArrayList<Scenario>();
    ArrayList<String> csvContents = new ArrayList<String>();

    ArrayList<Character> passengers = new ArrayList<Character>();
    ArrayList<Character> pedestrians = new ArrayList<Character>();

    try (BufferedReader csvReader = new BufferedReader(new FileReader(csvFile));) {
        String headerLine = csvReader.readLine(); //get rid of the header
        //add each line to the arrayList
        while ((line = csvReader.readLine()) != null) { 
            csvContents.add(line);   
        }

        for(String csvLine : csvContents) {                
            String[] data = csvLine.split(csvSplit); // split by comma and remove redundant spaces

            if (data.length == NO_OF_FIELD) { //check and avoid indexOutOfBoundException

                String clazz = data[0].toLowerCase();// cannot use word "class" as a variable

                if (clazz.startsWith("scenario") && data.length == 1) { 
                    scenario = new Scenario();
                    scenario.setLegalCrossing(clazz.endsWith("green"));
                    continue;
                } 

                else if ("person".equals(clazz) && data.length ==10) {

                    person = loadCsvPerson(data);
                    addCharacter(person, data);

                } 

                else if ("animal".equals(clazz) && data.length ==10) {

                    animal = loadCsvAnimal(data);
                    addCharacter(animal, data);

                    }                        
                }
            }                               
        }
        //passenger and pedestrians are in position
        System.out.println("passengers: " + passengers);
        System.out.println("pedestrians: " + pedestrians);

        if (null != scenario) {
            scenario.setPassengers(passengers);
            scenario.setPedestrians(pedestrians);
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } 
}
Woden :

I need to add the previous scenario in the second round. Since the last set of data won't be captured, I need to set another new scenario to add it in. Thanks for the art sir.

Character character = null;
    try (BufferedReader csvReader = new BufferedReader(new FileReader(csvFile));) {
        String headerLine = csvReader.readLine(); //get rid of the header
        //add each line to the arrayList
        while ((line = csvReader.readLine()) != null) { 
            csvContents.add(line);   
        }
        final int NO_OF_FIELDS = 10;

        for(String csvLine : csvContents) {                
            String[] data = csvLine.split(csvSplit); // split by comma and remove redundant spaces                
            String clazz = data[0].toLowerCase();// cannot use word "class" as a variable

            if (clazz.startsWith("scenario") && data.length == 1) {
                // adding scenario after one set of data
                // i.e second round adding the first round data
                if (passengers.size() != 0 && pedestrians.size() != 0) {
                    Scenario scenario = new Scenario();
                    scenario.setPassengers(passengers);
                    scenario.setPedestrians(pedestrians);
                    scenarios.add(scenario);
                }

                passengers = new ArrayList<Character>();
                pedestrians = new ArrayList<Character>();

                if (clazz.endsWith("green")) {
                    scenario.setLegalCrossing(true);
                    System.out.println("green light");
                }

                else if (clazz.endsWith("red")){
                    scenario.setLegalCrossing(false);
                    System.out.println("red light");
                } 

                continue;
            }
        //...
            Scenario scenario = new Scenario();
            scenario.setPassengers(passengers);
            scenario.setPedestrians(pedestrians);
            scenarios.add(scenario);

            scenario.setPassengers(passengers);
            scenario.setPedestrians(pedestrians);

            Audit audit = new Audit();
            audit.setScenario(scenarios);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

extract the same column (different length) from multiple .csv and cbind in one data frame - R

Parsing a csv file with column data in Python

Parsing CSV Data from one column with Pandas

Parsing variable length data

df.shape returns different column length for same dataframe in csv

Parsing different data types in a CSV file in C++?

Build column of data frame with character vectors of different length?

Change a value in a column in several data frames with different columns length in R

How to add a column of different length to a data frame in R?

CSV Reading and parsing data

Parsing CSV data

Length of longest stretch of NAs in a column of data-frame object

Matching columns of different csv files, not working when column value is different length

Export data to csv with different data types per column

Plot data with different length

JavaScript Parsing Data into an Object

get the length of first column in csv

Create new column based on two columns (different length) from different data frames

Comparision of 2 csv files having same column names with different data

How can i add a column to a dataframe based on a conditional of another dataframe that has a different length, but shared column data

What are different ways of parsing data

Spring batch parsing on different data

Parsing nested data from CSV

Uploaded CSV file parsing data

Parsing data from CSV file

convert data frame column containing different date formats to Date object

explode pandas data frame column of object type to string and into different rows

Python, Parsing Column Data, pandas

Set substring length with the length of longest data in column