Search code examples
javacsvapache-commons-csv

Getting Duplicate while trying to read CSV file with Apache Common CSV


I have a class that try to read a CSV file using Apache Common CSV, so far my code is working fine except that am not getting the result am expecting. My code is displaying a duplicate of the second column in the csv file as below:

[email protected]
google
google.com
[email protected]
google
tutorialspoint
[email protected]
google

My CSV File

Name,User Name,Password
google.com,[email protected],google
tutorialspoint,[email protected],google

i expect to get something like this:

google.com
[email protected]
google
tutorialspoint
[email protected]
google

Here is my block that parses the csv using Apache CSV

public List<String> readCSV(String[] fields) {
        // HERE WE START PROCESSING THE READ CSV CONTENTS
        List<String> contents = new ArrayList<String>();
        FileReader fileReader = null;
        CSVParser csvFileParser = null;

        // HERE WE START PROCESSING
        if(fields!=null){
            //Create the CSVFormat object with the header mapping
            CSVFormat csvFileFormat = CSVFormat.DEFAULT.withHeader(FILE_HEADER_MAPPING);

            try {

                //Create a new list of student to be filled by CSV file data
                List<String> content=new ArrayList<String>();

                //initialize FileReader object
                fileReader = new FileReader(FilePath);

                //initialize CSVParser object
                csvFileParser = new CSVParser(fileReader, csvFileFormat);

                //Get a list of CSV file records
                List<CSVRecord> csvRecords = csvFileParser.getRecords(); 

                //Read the CSV file records starting from the second record to skip the header
                for (int i = 1; i < csvRecords.size(); i++) {
                    CSVRecord record = csvRecords.get(i);
                    //Create a new student object and fill his data
                    for(int j=0; j<fields.length; j++){
                        content.add(record.get(fields[j]));
                    }
                    // Here we submit to contents
                    contents.addAll(content);
                    System.out.println(contents.size());
                } // end of loop
            } 
            catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    fileReader.close();
                    csvFileParser.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
            // Here we return
            return contents;
        }

I cant just figure out what am missing here, any help will be welcomed.


Solution

  • The reason is that you're adding the String list content each iteration

                    contents.addAll(content);
    

    Either clear content on each iteration or just change

                        content.add(record.get(fields[j]));
    

    to

                        contents.add(record.get(fields[j]));
    

    and remove the

                    contents.addAll(content);
    

    line