我的代码没有正确提取我的文本文件/数组列表?

7cjasjjr  于 2021-07-11  发布在  Java
关注(0)|答案(2)|浏览(288)

这个问题在这里已经有答案了

如何打印java对象而不获取“sometype@2f92e0f4”? (10个答案)
上个月关门了。
我的程序没有从从文本文件中提取的arraylist打印出详细信息。文本文件详细信息以共享一个相似性的数据库方式分为两部分。
我知道我把其他的课程都删掉了,但我想知道它在打印什么,或者是从哪里来的?
我的文本文件/arraylist的程序输出:
中等以上学生:courseworkstudent@53f65459 courseworkstudent@3b088d51
学生低于平均水平:courseworkstudent@1786dec2 courseworkstudent@74650e52 courseworkstudent@15d0c81b
主要客户

public class MainClient {
public static int choice;
public static Scanner kb = new Scanner(System.in);
ArrayList<Student> courses = new ArrayList<Student>();
ArrayList<Student> students = new ArrayList<Student>();
public void studentTxtRead() {//Reading of Student.txt 
    Scanner studFile = null;
    try {
    studFile = new Scanner(new File("Student.txt"));
        while(studFile.hasNextLine()){
        String line = studFile.nextLine();
        String[] data = line.split("\\s+");
        students.add(new Student(data[0], data[1], data[2], Long.parseLong(data[3])
        ,Integer.parseInt(data[4]),Integer.parseInt(data[5]), Integer.parseInt(data[6])));
        }//end of while loop
    }//end of try loop
    catch(FileNotFoundException ex){
        System.out.println("FILE NOT FOUND");
    }
    studFile.close();
}//end of studentTxtRead

public void courseworkStudentAvg(){
    double total = 0.0;
    int count = 0;
     for(int i = 0; i < courses.size(); i++){
         if(courses.get(i) instanceof CourseworkStudent) {
         CourseworkStudent courseW = (CourseworkStudent)courses.get(i);
         total += courseW.getCourseMarks();
         count++;
     }//end of if
}//end of for
     double avg = total/count;
     System.out.println("Students above averge: ");
     for(int i=0; i<courses.size(); i++) {
         if(courses.get(i)instanceof CourseworkStudent) {
             CourseworkStudent courseW =(CourseworkStudent) courses.get(i);
             if(courseW.getCourseMarks()> avg) {
                 System.out.println(courseW);
             }//end of if
         }//end of if
      }//end of for
     System.out.println("\nStudent below average: ");
     for(int i =0; i<courses.size(); i++) {
         if(courses.get(i) instanceof CourseworkStudent) {
             CourseworkStudent courseW = (CourseworkStudent) courses.get(i);
             if(courseW.getCourseMarks()< avg) {
                 System.out.println(courseW);
             }//end of if
         }//end of if
      }//end of for
}//end of courseworkStudentAvg

public void calculateCouGradeMarks(){//calculate grade/marks for coursework
       for(int i = 0; i < courses.size(); i++){// Calculate grades for Course Student
           if(courses.get(i) instanceof CourseworkStudent){
               CourseworkStudent courW = (CourseworkStudent) courses.get(i);
               courW.CGrading();
           }
       }           
}//end of CalculateCouGradeMarks
ckx4rj1h

ckx4rj1h1#

将tostring()方法添加到courseworkstudent类

8oomwypt

8oomwypt2#

您正在尝试打印的对象示例 CourseworkStudent . 这些对象被描述为。 CourseworkStudent@53f65459 .
这是因为 System.out.println() 无法将对象解释为 String . 所以它会打印一个对象标识符。
为了打印内容,您需要实现 toString() 函数,并精确地定义对象作为字符串的外观。

相关问题