簡體   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