繁体   English   中英

如何从文件中读取并使用读取文件中的输入创建新对象

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

读入学生文件。 为每个学生ID创建一个Student对象。 将该对象名称设置为文件中学生ID后面的名称。 将学生对象添加到以学生ID为键的地图上。 阅读课程文件。 对于每个学生ID,从地图中查找Student对象。 阅读文件中的学分时数行。 阅读文件中的成绩线。 使用学时和成绩创建“课程”对象。 将该课程对象添加到学生对象的课程集合中。

这是我的代码,它从文件中读取信息:

    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);

我完全不知道该怎么做。

我知道如何创建学生对象:

Student student1 = new Student();

但是,根据读入的信息,我需要每个名称“ student1”都不同。

例如,如果我读“ 001”和“朱莉·琼斯”,我希望我的学生对象是

Student student1 = new Student();

然后是下一个

Student student2 = new Student();

对于学生studenti = new Student();,其中i =从文件中读取的学生ID的数量。

,我认为这个问题有点误导。 “对象名称”是指学生姓名,而不是对象引用变量的名称。 我正在读的是,您将需要创建一个学生对象,并将其名称作为参数传入。

我认为应该是这样的(伪代码):

//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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM