0

I need to read from a CSV file in Java, and add the information to a list of string arrays. The problem is the code gives an error while reading the CSV file, but if I take out the CSV column named "Synopsis", the code runs perfectly. It's clear the column has something to do with the codes failure, but I can't figure out what. Anyone have any ideas?

Link to google sheets version of the CSV https://docs.google.com/spreadsheets/d/1EO243KiaZ_uxEKF1mvozONHvTBm5HfFsUvrR5SSB238/edit?usp=sharing

Code here -->

package testerproject;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class testerpage {
    private List<String[]> list = new ArrayList();
    testerpage(){
        try {
            animeList();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
    private void  animeList() throws IOException {
        File file = new File("resources/CSVFiles/animeProjectData.csv");
        int count = 0; //keep track of where in the list new line is added
        try{
            BufferedReader br = new BufferedReader(new FileReader(file));
            String line = "";
            //add line from CSV file to specified list index
            while ((line = br.readLine()) != null) {
                list.add(count,line.split(",")); 
                count++;
                }
            } catch (FileNotFoundException e) {
        }           
        System.out.println(list.get(2)[3]);
    }
}

Errors given ->

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 1
    at testerproject.testerpage.animeList(testerpage.java:37)
    at testerproject.testerpage.<init>(testerpage.java:17)
    at testerproject.tester.main(tester.java:6)
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 1
    Please include the minimal CSV necessary to reproduce the problem in your question as code formatted text. – Mark Rotteveel Jul 04 '21 at 10:30
  • 1
    You cannot just read the rows line by line and split the rows by comma `,` because `synopsis` column is a multiline text containing commas. You should be using some library to read CSV files instead of your simplified approach. Link: [Any good library to read and write CSV files](https://stackoverflow.com/questions/10462507/any-good-library-to-read-and-write-csv-files) – Nowhere Man Jul 04 '21 at 10:38

2 Answers2

1

Use opencsv to read csv file , I tried the csv file you shared an was able to read it without any errors using opencsv, more reference at https://www.baeldung.com/opencsv

import com.opencsv.CSVReader;

public class testerpage {
    private List<String[]> list = new ArrayList();
    testerpage(){
        try {
            animeList();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
    private void  animeList() throws IOException {
        File file = new File("resources/CSVFiles/animeProjectData.csv");
        int count = 0; //keep track of where in the list new line is added
        try{
            Reader reader = Files.newBufferedReader(file.toPath());
            CSVReader csvReader = new CSVReader(reader);
            String[] line;
            while ((line = csvReader.readNext()) != null) {
                list.add(line);
                count++;
            }
            reader.close();
            csvReader.close();
            } catch (FileNotFoundException e) {
        }           
        System.out.println(list.get(2)[3]);
    }
}
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
anish sharma
  • 568
  • 3
  • 5
0

Your code is fine the problem - I believe - is in the commas that is at C:3 [2][3] in the spread sheet you shared here

.
.
.
when he meets a beautiful violinist, Kaori Miyazono, who stirs up his 
           
world and sets him on a journey to face music again. 
 
Based on the manga series of the same name,  Shigatsu wa Kimi no 
                                                              
Uso  approaches the story of Kousei's recovery as he discovers that 
.
.
.

This file cannot be split by commas I recommend converting the sheet to a TSV ( tab seperated ) or other delimiter instead of comma, maybe something like a bar symbol | ?, since you're using commas inside the colum's contents.

SaleemKhair
  • 499
  • 3
  • 12
  • Thanks for the future heads up, I think I found the bigger problem though which was the "Synopsis" column seems to be fixed width instead of delimited. I think that messed things up – Joeystandard Jul 04 '21 at 12:14