2017-04-11 110 views
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(); 
    } 
} 

回答

0

你重新创建的结果阵列每次迭代通过循环读取CSV文件:

finalStudentGradesArray =新的ArrayList();

+0

是的。移动'finalStudentGradesArray = new ArrayList ();'在while循环之外或将其移动到构造函数中。 –

+0

@David谢谢你,我刚刚在读取器循环之外实例化ArrayList,并删除了每行迭代创建一个新ArrayList的代码行。非常感谢大卫,这为我解决了这个问题。 –

相关问题