簡體   English   中英

遍歷對象的ArrayList並打印每個對象中的每個變量

[英]Iterating through an ArrayList of Objects and printing each variable from each object

您好,我在打印對象的ArrayList時遇到問題。 我想從CSV文件中導入值(此方法有效),然后將值保存到我創建的對象(StudentInfo)中,然后想遍歷並打印出每個對象的信息。

我已經研究過並且無法解決此問題,我在代碼中進行了一些更改,並且能夠按預期方式打印一個對象,並且這似乎是存儲在ArrayList中的最后一個對象。

我跟蹤了我的代碼(在Eclipse中使用調試器),發現它只進入了while循環一次,而我不明白為什么。

我相信問題出在我的printResults()方法中,盡管我無法弄清楚為什么它只進入while循環一次(而且正如我之前所說的,似乎只是ArrayList的最后一個索引)。

請參見以下代碼塊。 感謝您抽出寶貴的時間對此進行審查,也感謝您可能提供的任何幫助。 這是家庭作業,因此我們被告知要使用令牌生成器(盡管教授說,將來會有更好的方法來學習)。

package excercise12Dot1;

public class StudentInfo {
    private String type;
    private String fName;
    private String lName;
    private double testOne;
    private double testTwo;
    private double testThree;
    private double finalGrade;

    public StudentInfo() {
        this.type = "";
        this.fName = "";
        this.lName = "";
        this.testOne = 0;
        this.testTwo = 0;
        this.testThree = 0;
        this.finalGrade = 0;
    }

    public StudentInfo(String type, String fname, String lName, double testOne, double testTwo, double testThree, double finalGrade){
        this.type = type;
        this.fName = fname;
        this.lName = lName;
        this.testOne = testOne;
        this.testTwo = testTwo;
        this.testThree = testThree;
        this.finalGrade = finalGrade;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getfName() {
        return fName;
    }

    public void setfName(String fName) {
        this.fName = fName;
    }

    public String getlName() {
        return lName;
    }

    public void setlName(String lName) {
        this.lName = lName;
    }

    public double getTestOne() {
        return testOne;
    }

    public void setTestOne(double testOne) {
        this.testOne = testOne;
    }

    public double getTestTwo() {
        return testTwo;
    }

    public void setTestTwo(double testTwo) {
        this.testTwo = testTwo;
    }

    public double getTestThree() {
        return testThree;
    }

    public void setTestThree(double testThree) {
        this.testThree = testThree;
    }

    public double getFinalGrade() {
        return finalGrade;
    }

    public void setFinalGrade(double finalGrade) {
        this.finalGrade = finalGrade;
    }
}

package excercise12Dot1;
import java.io.File;
import java.util.StringTokenizer;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

public class NonCreditCourseGrades {
    private Scanner csvReader;
    private int counter = 0;
    private ArrayList<StudentInfo> finalStudentGradesArray;
    public void openFile(){
        try{
            csvReader = new Scanner(new File("StudentsScores.csv"));
            while (csvReader.hasNext()){
                String studentRecord = csvReader.nextLine();
                StringTokenizer tokenizer = new StringTokenizer(studentRecord, ",");
                String type = tokenizer.nextToken();
                String fName = tokenizer.nextToken();
                String lName = tokenizer.nextToken();
                double testOne = Double.parseDouble(tokenizer.nextToken());
                double testTwo = Double.parseDouble(tokenizer.nextToken());
                double testThree = Double.parseDouble(tokenizer.nextToken());
                double finalScore = 0;
                StudentInfo newStudent = new StudentInfo(type, fName, lName, testOne, testTwo, testThree, finalScore);
                finalStudentGradesArray = new ArrayList<StudentInfo>();
                finalStudentGradesArray.add(newStudent);
                ++counter;
            }   
        }catch(FileNotFoundException ex){
            System.err.println("FILE NOT FOUND");
        }
        csvReader.close();
    }
    public void printResults(){
        Iterator<StudentInfo> itr = finalStudentGradesArray.iterator();
        while (itr.hasNext()){
            StudentInfo results = (StudentInfo)itr.next();
            System.out.println("Student Type:\t" + results.getType() + "\nFirst Name:\t" + results.getfName() + "\nLast Name:\t" + results.getlName() + "\nTest 1:\t\t" + results.getTestOne() + "\nTest 2:\t\t" + results.getTestTwo() + "\nTest 3:\t\t" + results.getTestThree() + "\nFinal Score: \t" + results.getFinalGrade() + "\n\n");
        }
    }

package excercise12Dot1;


public class NonCreditCourseGradesRunner {

    public static void main(String[] args) {
        NonCreditCourseGrades test = new NonCreditCourseGrades();
        test.openFile();
        test.printResults();
    }
}

您通過循環讀取CSV文件,在每次迭代中都重新創建了結果數組:

finalStudentGradesArray = new ArrayList();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM