简体   繁体   中英

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

Hello I am having a problem with printing an ArrayList of objects. I would like to import the values from a CSV file (this works) and then save the values into an object I created (StudentInfo) and then I would like to iterate through and print out each objects information.

I have researched and was unable to solve this, I changed a few things in my code and I am able to get one object to print as expected and that seems to be the last object stored into the ArrayList.

I have traced my code (using debugger in Eclipse) and found it is only entering the while loop one time, and I cant understand why.

I believe the problem is within my printResults() method although I cant figure out why it is only going into the while loop one time (and as I said earlier seems to be only the last index of the ArrayList).

Please see the below blocks of code. Thank you for taking the time to review this and thank you for any assistance you may be able to provide. This is homework so we were told to use tokenizer (although the professor said there are better ways that we will learn in the future).

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

You recreated the results array every iteration through the loop reading the CSV file:

finalStudentGradesArray = new ArrayList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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