迭代对象的ArrayList并打印每个对象的每个变量

时间:2017-04-11 11:13:32

标签: java arraylist iterator

您好我在打印对象的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();
    }
}

1 个答案:

答案 0 :(得分:0)

您在读取CSV文件的循环中每次迭代都重新创建结果数组:

finalStudentGradesArray = new ArrayList();