Jonathan Dewein
Jonathan Dewein

Reputation: 984

How to read in from a file and create new objects with input from the read file

Read in students file. For each student ID create a Student object. Set that objects name to the name in the file after the student ID. Add the Student object to the map with the student ID as the key. Read in courses file. For each student ID, lookup the Student object from the map. Read the credit hour line in the file. Read the grade line in the file. Create a Course object using the credit hour and grade. Add that Course object to the Student object's collection of courses.

Here is my code which reads in the information from the file:

    FileReader freader = new FileReader(nameFile);
    BufferedReader Breader = new BufferedReader(freader);
    boolean end = Breader.ready();

        do {
            next = Breader.readLine();
            sNumber = Integer.parseInt(next);
            formatSNumber = String.format("%03d", sNumber);
            //Assignment the formatted number to my HashMap
            sName = Breader.readLine();
            //Assignment the name to my HashMap
            end = Breader.ready();
        } while(end);

I am completely lost on how to do this.

I know how to create a student object:

Student student1 = new Student();

However, I need each name, "student1", to be different depending upon the information read in.

For example, if I read "001" and "Julie Jones", I want my student object to be

Student student1 = new Student();

And then the next one to be

Student student2 = new Student();

For Student studenti = new Student();, where i = the number of student IDs read in from the file.

Upvotes: 0

Views: 235

Answers (1)

Nick Ziebert
Nick Ziebert

Reputation: 1268

Yo, I think the question is a bit misleading. "objects name" means students name-- not the name of the objects reference variable. What I'm reading is that you will need to create a student object with their Name passed in as a parameter.

I think it should be something like this (psudocode):

//create a map//
for each line in file {
    int id=//GET THE ID//
    String name=//GET THE STUDENTS NAME//
    Student student=new Student(name);
    map.add(student, id);
}

Upvotes: 1

Related Questions