Search code examples
javaopencsv

Java store specific csv value to list


The CSV look like this:

    Name;Amount;Date
    Netflix;5;1.1.2021

I want a different list for each expense, one for entertainment one for transport etc. However, I only want the amount to be stored on a list, how would I do that?

public class CsvReader {
    public static void readDataLineByLine(String file) {
        try {
            // Create an object of file reader class with CSV file as a parameter.
            FileReader filereader = new FileReader(file);

            // create csvParser object with
            // custom separator semi-colon
            CSVParser parser = new CSVParserBuilder().withSeparator(';').build();

            // create csvReader object with parameter
            // filereader and parser
            CSVReader csvReader = new CSVReaderBuilder(filereader).withCSVParser(parser).build();

            // Read all data at once
            List<String[]> allData = csvReader.readAll();
            List<String> entertainment = new ArrayList<>();

            // Print Data.
            for (String[] row : allData) {
                for (String cell : row) {
                    System.out.print(cell + "\t");
                    if (cell.startsWith("Netflix")){
                        entertainment.add(cell);
                    }
                }
                System.out.println();
                System.out.println(entertainment);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        CsvReader.readDataLineByLine("tt.csv");
    }
}

Solution

  • If you use opencsv , how about writing the code like below? Please review.

    public class CsvReader {
        public static void readDataLineByLine(String file) {
            try {
                // Create an object of file reader class with CSV file as a parameter.
                FileReader filereader = new FileReader(file);
    
                // create csvParser object with
                // custom separator semi-colon
                CSVParser parser = new CSVParserBuilder().withSeparator(';').build();
    
                // create csvReader object with parameter
                // filereader and parser
                CSVReader csvReader = new CSVReaderBuilder(filereader).withCSVParser(parser).build();
    
                List<String> entertainment = new ArrayList<>();
                // changed part 
                int index = 0;
                while ((nextLine = reader.readNext()) != null) {   // 2
                    // csv header exclusion condition
                    if(index == 0) {
                       continue;
                    }
                    String name = nextLine[0];
                    String amount = nextLine[1];
                    if (name.startsWith("Netflix")){
                        entertainment.add(amount);
                    }
                    index++;
                }
                // Print Data.
                System.out.println();
                System.out.println(entertainment);
                
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        public static void main(String[] args) {
            CsvReader.readDataLineByLine("tt.csv");
        }
    }