0
你好,我遇到了打印對象ArrayList的問題。我想從一個CSV文件中導入這些值(這是可行的),然後將這些值保存到我創建的對象(StudentInfo)中,然後我想遍歷並打印出每個對象的信息。遍歷對象的ArrayList並打印每個對象的每個變量
我已經研究過,無法解決這個問題,我在我的代碼中改變了一些東西,我能夠得到一個對象來按預期方式打印,並且似乎是存儲到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();
}
}
是的。移動'finalStudentGradesArray = new ArrayList();'在while循環之外或將其移動到構造函數中。 –
@David謝謝你,我剛剛在讀取器循環之外實例化ArrayList,並刪除了每行迭代創建一個新ArrayList的代碼行。非常感謝大衛,這爲我解決了這個問題。 –