I'm almost done with my assignement
I've created 2 class and I have one format method in student class
the file look like this (there are about 25 lines)
courseNum
courseName
courseSemester
studentID,studentProgram,studentGpa
studentID,studentProgram,studentGpa

the output should look like this
courseNum
courseName
courseSemester
studentID--studentProgram--studentGpa
studentID--studentProgram--studentGpa

I'm still not sure if my code are correct but I want to know how can I return ArrayList

private List<Student> createStudent(String fileName) {

        String[] data = FileUtils.readIntoArray(fileName);
        List<Student> res = new ArrayList();
        List<Course> a = new ArrayList();

        for (int i = 0; i < data.length; i++) {
            String line = data[i];
            String num = data[0]; 
            String name = data[1];
            String semester = data[3];   
            String[] p = line.split(","); 
            int id = Integer.parseInt(p[4]);
            String program = p[5];
            double gpa = Double.parseDouble(p[6]);
            List<Student> s = (List<Student>) new Student(id, program, gpa);
            List<Course> c =  (List<Course>) new Course(num, name, semester, s); 
            res [i] = c; 
        }
        return res; 
        }

I want to know how can I return ArrayList

You are actually doing this already.
In the code that you posted on line 4 you let res be a reference to a List, and set it to refer to an ArrayList.
On line 20 you have return res; which will return a reference to that ArrayList.

I'm still not sure if my code are correct

Change this:

List<Student> res = new ArrayList();
List<Course> a = new ArrayList();

to:

List<Student> res = new ArrayList<Student>();
List<Course> a = new ArrayList<Course>();

or (as of Java 7) you can also let the compiler infer the type parameter using diamond syntax:

List<Student> res = new ArrayList<>();
List<Course> a = new ArrayList<>();

in order to fix the compiler warning that will come up if you actually try to compile.

The following:

List<Student> s = (List<Student>) new Student(id, program, gpa);
List<Course> c =  (List<Course>) new Course(num, name, semester, s);

will fail at runtime due to a ClassCastException (unless Student and Course would be List implementations, which is highly unlikely, and plain wrong)

Also:

res [i] = c;

will fail at compile time because res is not an array reference.

Let me requote you:

I'm still not sure if my code are correct

I hope we can now both see that your code is incorrect and that you should've compiled and tried to fix it before posting it down here.
Merely thinking that it is correct is not sufficient, if you are not sure, try to compile it, if it doesn't, try to fix it. Once it is fixed, try to run it, if it breaks fix it again.

to return an ArrayList you just have to have your return type as arraylist

private ArrayList<Student> getStudent(){
    ArrayList<Student> myArrayList = new ArrayList<Student>();

    return myArrayList;
}

as an example

to return an ArrayList you just have to have your return type as arraylist

Not necessarily, you can specify a superinterface or superclass as the method's return type and return a reference to an implementation of it.
ArrayList is an implementation of the List interface so it is guaranteed to have the methods specified by that interface.

I got red line under res [i] = c; it say array required, but List<Student> found .. how can I fix it

my method is type List<Student> but I want the result to be num, name, semester, s but the type is List<Course> and the return shoudl be type List<Student>..

I have a couple of questions:

  1. You defined the input file structure as follows:
    courseNum
    courseName
    courseSemester
    studentID,studentProgram,studentGpa
    studentID,studentProgram,studentGpa

    Can that whole block be repeated multiple times throughout the file?

  2. FileUtils.readIntoArray(fileName); what result does it produce?
    Does it produce an array containing the lines of the file, where each array element holds one line?
    If you don't know and have the implementation, could you post it?

of qu

of qu

fined the input fil

fined the input fil

the input file

the input file

hat whole block be

block be repeated multiple times throughout the file?

block be repeated multiple times throughout the file?

I have a couple of questions:

You defined the input file structure as follows:
courseNum
courseName
courseSemester
studentID,studentProgram,studentGpa
studentID,studentProgram,studentGpa

Can that whole block be repeated multiple times throughout the file?

FileUtils.readIntoArray(fileName); what result does it produce?
Does it produce an array containing the lines of the file, where each array element holds one line?
If you don't know and have the implementation, could you post it?

I have a couple of questions:

You defined the input file structure as follows:
courseNum
courseName
courseSemester
studentID,studentProgram,studentGpa
studentID,studentProgram,studentGpa

Can that whole block be repeated multiple times throughout the file?

FileUtils.readIntoArray(fileName); what result does it produce?
Does it produce an array containing the lines of the file, where each array element holds one line?
If you don't know and have the implementation, could you post it?

I have a couple of questions:

You defined the input file structure as follows:
courseNum
courseName
courseSemester
studentID,studentProgram,studentGpa
studentID,studentProgram,studentGpa

Can that whole block be repeated multiple times throughout the file?

FileUtils.readIntoArray(fileName); what result does it produce?
Does it produce an array containing the lines of the file, where each array element holds one line?
If you don't know and have the implementation, could you post it?

for the first question > this line >> studentID,studentProgram,studentGpa will be repeated more than once in the file . the file hold information about one unique course (name num, semester) diffrenet information about the student where the ID number is diffrenet

courseNum
courseName
courseSemester
studentID,studentProgram1,studentGpa2
studentID1,studentProgram3,studentGpa3
studentID2,studentProgram1,studentGpa
studentID3,studentProgram4,studentGpa2
studentID4,studentProgram5,studentGpa6

for the second question yes it produce an array containing the lines of the file, where each array element holds one line

also I have this before the method

public class JFrameHw extends javax.swing.JFrame {

  List<Student> student = new ArrayList();
    public JFrameHw() {
        initComponents();
                // get the file name from the textbox 
                student = createStudent(txtFile.getText()); 
    }

I rewrote your code somewhat, and added comments so that you can see what is going on:
I did not test this code, but it should give you a basic idea.

private List<Student> createStudents(String filename) {
    /*
     * Read the file contents, putting each line into an array element.
     *
     * Assuming a valid file format, the array will contain:
     * [0] -> courseNum
     * [1] -> courseName
     * [2] -> courseSemester
     * [3]..[N] -> line representing student
     */
    String[] data = FileUtils.readIntoArray(filename);

    /* Create an ArrayList to which all students will be added as the array is processed. */
    List<Student> students = new ArrayList<Student>();

    /*
     * Create a Course object representing the unique course.
     * A reference to the (currently empty) array of Students is passed.
     * Note that this object is not used for anything further in this code.
     * See the question at the end of my post.
     */
    Course c = new Course(data[0], data[1], data[2], students);

    /*
     * Create a Student for each remaining line in the file.
     * A line that represents a Student has the following format:
     * 
     * studentID,studentProgram,studentGpa
     *
     * Note that the for needs to start at index 3 because the
     * indexes zero to two (included) hold information about the course.
     */
    for (int i = 3; i < data.length; i++) {
        /*
         * Split up the line representing a Student using comma as a delimiter.
         *
         * Assuming a valid file format, the array will contain:
         * [0] -> studentID
         * [1] -> studentProgram
         * [2] -> studentGpa
         */
        String[] p = line.split(",");

        /* Parse the Student data to the correct type where necessary. */
        int studentId = Integer.parseInt(p[0]);
        String studentProgram = p[1];
        double studentGpa = Double.parseDouble(p[2]);

        /* Create a Student and add it to the List of Students. */
        students.add(new Student(studentId, studentProgram, studentGpa));
    }

    /* Return the list of Students */
    return students;
}

I'm wondering... are you required to return a List of Students, or a single Course object that holds a List of students?
Depending on the answer you should be able to implement it yourself if you study the above code.

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.