Search code examples
javaparsingarrayliststringtokenizer

Separate data and store in ArrayList?


I am trying to separate a set of data. I need to store each data into a variable then create an object and put them in an ArrayList.

The dataset is in this format

lastName, firstName
ID num_courses
course1_name 
grade units
course2_name
grade units
....

I have done this so far but I'm having trouble creating multiple objects, storing the data, and adding it to an ArrayList[]. Thanks for your time and help (:

public static void main (String[] args) throws FileNotFoundException {
Scanner scnr = new Scanner(System.in);
System.out.println("Enter file name: ");
String fileName = scnr.nextLine();
Scanner inFile = new Scanner(new FileInputStream(fileName)); //importing filename

ArrayList<String> list = new ArrayList<String>(); //for list of courses
String name = "";
String id = "";
int coursesTaken = 0;
String courseName = "";
char grade;
int courseUnit = 0;

while(inFile.hasNextLine()){
    name = inFile.nextLine(); //first line is name
    StringTokenizer st = new StringTokenizer(inFile.nextLine()); //separting whitespace
    id = st.nextToken(); //storing ID
    coursesTaken = Integer.parseInt(st.nextToken()); //storing num_courses_taken

    Student s1 = new Student(name, id); //FIXME (need more than 1 student object)

    for(int i = 0; i < coursesTaken*2; ++i){
        courseName = inFile.nextLine();
        s1.addCourses(courseName); //adding to arrayList

        StringTokenizer st2 = new StringTokenizer(inFile.nextLine());
        grade = st2.nextToken().charAt(0);
        courseUnit = Integer.parseInt(st2.nextToken());

        Course ci = new Course(courseName, grade, courseUnit); //FIXME (need more than 1 course)


        }

    }
}

Course.java

public class Course {
    private String name;
    private char grade;
    private int units;


    //constructors
    public Course(){
        name = "";
        grade = 0;
        units = 0;
    }

    public Course(String name, char grade, int units){
        this.name = name;
        this.grade = grade;
        this.units = units;
    }

    //getters 
    public String getName(){
        return name;
    }

    public char getGrade(){
        return grade;
    }

    public int getUnits(){
        return units;
    }

    //setters
    public void setName(String name){
        this.name = name;
    }

    public void setGrade(char grade){
        this.grade = grade;
    }

    public void setUnits(int units){
        this.units = units;
    }  
}

Student.java

public class Student {

    private String name;
    private String id;
    private ArrayList<String> listCourses = new ArrayList<String>();

    //constructor
    //default
    public Student(){
        name = "none";
        id = "none";
        //courses = "none";
    }

    public Student(String name, String id){
        this.name = name;
        this.id = id;
        //this.courses = courses;
    }


    //getters
    public String getName(){
        return name;
    }

    public String getId(){
        return id;
    }

    public ArrayList<String> getCourses(){
        return listCourses;
    }


    //setters
    public void setName(String name){
        this.name = name;
    }

    public void setId (String id){
        this.id = id;
    }

    public void addCourses(String courses){
        listCourses.add(courses);
    }
}

Solution

  • First, you need a List to store your students:

    ArrayList<Student> students = new ArrayList<>();
    

    You should add each created student to this list when all fields are parsed:

    students.add(s1);
    

    Second, I think, it's better to change the listCourses field from ArrayList<String> to ArrayList<Course> - to store information about all students courses inside the Student object.

    Third, you don't need to multiply coursesTaken by 2 because you call inFile.nextLine() twice for each course taken.

    for (int i = 0; i < coursesTaken; i++)
    

    Finally your main method would look like this:

    private ArrayList<Course> listCourses = new ArrayList<>();
    
    ArrayList<Student> students = new ArrayList<>();
    
    while(inFile.hasNextLine()){
        name = inFile.nextLine(); //first line is name
        StringTokenizer st = new StringTokenizer(inFile.nextLine()); //separting whitespace
        id = st.nextToken(); //storing ID
        coursesTaken = Integer.parseInt(st.nextToken()); //storing num_courses_taken
    
        Student s1 = new Student(name, id);
        ArrayList<Course> courses = new ArrayList<>();
    
        for(int i = 0; i < coursesTaken; i++){
            courseName = inFile.nextLine();
    
            StringTokenizer st2 = new StringTokenizer(inFile.nextLine());
            grade = st2.nextToken().charAt(0);
            courseUnit = Integer.parseInt(st2.nextToken());
    
            courses.add(new Course(courseName, grade, courseUnit)); //FIXED
    
        }
        s1.setListCourses(courses);
        students.add(s1); //FIXED
    
    }
    

    UPDATE: Changes in Student class:

    public class Student {
        private String name;
        private String id;
        private ArrayList<Course> listCourses = new ArrayList<>();
        ...
    
        // this is just a setter for listCourses field
        public void setListCourses(ArrayList<Course> listCourses) {
            this.listCourses = listCourses;
        }
    }