-1

Student asking for help.

I have a CSV file in the format (studentID,lastName,firstName,finalMark,finalGrade). I am trying to read this into a List of type Student / ArrayList of type Student - trying to add each line as a new Student. I have a Student class implementing Comparable for sorting the file. This Student class has these five (studentID,lastName,firstName,finalMark,finalGrade) as the Constructor.

Can someone please demonstrate/show me, through code and/or explanation, how to read from the file, into the List using FileReader and BufferedReader or (second preference) Scanner.

public static ArrayList <String> readAllLinesFromFile(String path) throws IOException {

        // System.out.println("Cannot locate input file");

        ArrayList<String> studentList = new ArrayList<String>();
        FileReader fileReader = new FileReader("Question4unorderedList.csv");
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        String line = null;
        while((line = bufferedReader.readLine())!=null) {
            studentList.add(line);
        }
        bufferedReader.close();
        return studentList;
    }

So I can create an ArrayList of type String. But how can I convert/parse/translate this ArrayList of Strings into a List or ArrayList of Student (my Student Class Constructor details are above).

Thanks for your help.

4
  • What did you try so far? Any error you faced? Commented Apr 1, 2018 at 14:44
  • 2
    That's not the way it works: you haven't even try to do it. There are plenty of example on internet.Try coding before asking for something. Commented Apr 1, 2018 at 14:45
  • Take a look at this - stackoverflow.com/questions/42170837/… Commented Apr 1, 2018 at 14:45
  • reading csv could be a nightmare. search for a lib on internet and try it. Commented Apr 1, 2018 at 15:05

1 Answer 1

3
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main {

public static void main(String[] args) throws IOException {

    List <Student> studentList = new ArrayList <>();
    String fileIn = "Question4unorderedList.csv";
    String fileOut = "Question4orderedList.csv";
    String line = null;

    // Read all lines in from CSV file and add to studentList
    FileReader fileReader = new FileReader(fileIn);
    BufferedReader bufferedReader = new BufferedReader(fileReader);

    while ((line = bufferedReader.readLine()) != null) {
        String[] temp = line.split(",");
        int studentID = Integer.parseInt(temp[0]);
        String firstName = temp[1];
        String lastName = temp[2];
        int finalMark = Integer.parseInt(temp[3]);
        String finalGrade = temp[4];
        studentList.add(new Student(studentID, firstName, lastName, finalMark, finalGrade));
    }
    bufferedReader.close();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.