Read a CSV file and create an object with its data

0

I need a little help. I have a CSV file and I am reading it like this:

public class LectorDeArchivosCSV {
private List<Alumno> listaAlumnos;

public LectorDeArchivosCSV() {
    listaAlumnos = new ArrayList<>();
}

public List<Alumno> getListaAlumnos() {
    return listaAlumnos;
}

public static void leerCsv() {
    CSVReader csvReader = null;

    try {
        /**
         * Reading the CSV File
         * Delimiter is comma
         * Start reading from line 1
         */
        csvReader = new CSVReader(new FileReader("listadoDeAlumnos.csv"), ',', '"', 1);
        //employeeDetails stores the values current line
        String[] alumnoDetails = null;
        //List for holding all the rows
        List<String[]> rows = new ArrayList<String[]>();
        rows = csvReader.readAll();
        //Read individual row from List of rows
        for (String[] row : rows) {
            System.out.println(Arrays.toString(row));

        }

    } catch (Exception ee) {
        ee.printStackTrace();
    } finally {
        try {
            //closing the reader
            csvReader.close();
        } catch (Exception ee) {
            ee.printStackTrace();
        }
    }

}

This is how each row of the csv file looks

[1, Martin, Lopez]

The Integer being the Student code, String name and String last name. I need for each row to create an object of type Student with this data (I already have the constructor done).

    
asked by Bautista Querejeta 16.04.2018 в 00:19
source

1 answer

0

I do not know how your student constructor is, but ideally it's like that, be it Public student (String [] args) or at least overload it. In this way you will be able to directly instantiate Student object by passing the row (all strings) from your csv reader.

Greetings.

David

    
answered by 16.04.2018 / 00:43
source