hi I'm working on my hw and I have problem
I know how to read file from data and split each line using array

now I want to use arrayList .. I want to read the data from the file and create arrayList and then split each line and create object of type student
the file contanin id,program,gpa

I have Student class and the main form
in the student class I have format method , get and set methods

and this constructor

public Student(int id, String program, double gpa) {
        this.id = id;
        this.program = program;
        this.gpa = gpa;
    } 









//this is my main form 

package hw;

import java.util.ArrayList;
import java.util.List;
import utilities.FileUtils;



public class JFrameHw extends javax.swing.JFrame {

    List<Student> student = new ArrayList();

    public JFrameHw() {
        initComponents();
                student = createStudent(txtFile.getText());

    }

// This is my method 

 public List<Student> createStudent(String fileName) {
        String[] data = FileUtils.readIntoArray(fileName);
        List<Student> res = new ArrayList();
        for (int i = 0; i < student.size(); i++) {
            Student line = student.get(i);
            //Student line = (Student) list;

          }
        return res;

    }

Here, maybe this can help you:

    public void loadStudentsFromFile(){
        BufferedReader in = null;
        try{
            in = new BufferedReader(new FileReader(filename));
            String line;
            while ((line = in.readLine()) != null){
                String tokens[] = line.split(",");
                studs.add(new Student(Integer.parseInt(tokens[0]),tokens[1], Double.parseDouble(tokens[2])));
            }
        } catch (IOException e){
            System.out.println(e.getMessage());
        }
    }

where studs is:

private ArrayList<Student> studs = new ArrayList<Student>();

As a simple testing stuf:
Having in your Student class a function toString() like this:

    public String toString(){
        return new String("Id: "+Integer.toString(id)+", Program: "+program+", GPA: "+Double.toString(gpa));
    }

and a filename containing this:

1,Program1,5.6
2,Program2,6.7
3,Program3,7.9

would provide this output (if you use to print all the elements from the array using the toString() function):

Id: 1, Program: Program1, GPA: 5.6
Id: 2, Program: Program2, GPA: 6.7
Id: 3, Program: Program3, GPA: 7.9

And the printing function, easilly put:

    public void showStudents(){
        for (Student i:studs) System.out.println(i.toString());
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.