【问题标题】:Attempting to call an array and setting the value from a text file尝试调用数组并从文本文件中设置值
【发布时间】:2013-11-18 16:07:39
【问题描述】:

这里的这个类正在创建一个学生对象:

public class Student {
    String firstName;
    String lastName;
    int assignmentScores[];
    int labScores[];
    int attendanceScore;
    int totalHomeworkScore;
    int midterm1;
    int midterm2;
    int finalExam;
    int zyanteScore;
    int patScore;
    int totalTestScore;
    String letterGrade;

    public Student() {

    }

    public void setFirstName(String fName) {
        firstName = fName;

    }

    public String getFirstName() {
        return firstName;
    }

    public void setLastName(String lName) {

        lastName = lName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setAssignmentScores(int[] assignmentScore) {
        assignmentScores = assignmentScore;
    }

    public int[] getAssignmentScores() {
        return assignmentScores;
    }

    public void setLabScores(int[] labScore) {
        assignmentScores = labScore;
    }

    public int[] getLabScores() {
        return labScores;
    }

    public void setAttendanceScore(int attenScore) {
        attendanceScore = attenScore;
    }

    public int getAttendanceScore() {
        return attendanceScore;
    }

    public void setTotalHomeworkScore(int hScore) {
        totalHomeworkScore = hScore;
    }

    public int getTotalHomeworkScore() {
        return totalHomeworkScore;
    }

    public void setMidTerm1(int mT1) {
        midterm1 = mT1;
    }

    public int getMidterm1() {
        return midterm1;
    }

    public void setMidterm2(int mT2) {
        midterm2 = mT2;
    }

    public int getMidterm2() {
        return midterm2;
    }

    public void setFinalExam(int fExam) {
        finalExam = fExam;
    }

    public int getFinalExam() {
        return finalExam;
    }

    public void setZyanteScore(int zyant) {
        zyanteScore = zyant;
    }

    public int getZyanteScore() {
        return zyanteScore;
    }

    public void setPatScore(int pat) {
        patScore = pat;
    }

    public int getPatScore() {
        return patScore;
    }

    public void setTotalTestScore(int tScore) {
        totalTestScore = tScore;
    }

    public int getTotalTestScore() {
        return totalTestScore;
    }

    public void computeGrade() {
        if (getTotalHomeworkScore() <= 599 || getTotalTestScore() <= 149
                || getTotalHomeworkScore() <= 719 && getTotalTestScore() <= 179
                || getTotalHomeworkScore() <= 779 && getTotalTestScore() <= 164
                || getTotalHomeworkScore() <= 659 && getTotalTestScore() <= 209) {
            letterGrade = "P";
        }

        if (getTotalHomeworkScore() >= 1140 && getTotalTestScore() >= 180
                || getTotalHomeworkScore() >= 1080
                && getTotalTestScore() >= 195 || getTotalHomeworkScore() >= 960
                && getTotalTestScore() >= 210 || getTotalHomeworkScore() >= 900
                && getTotalTestScore() >= 225 || getTotalHomeworkScore() >= 840
                && getTotalTestScore() >= 240 || getTotalHomeworkScore() >= 780
                && getTotalTestScore() >= 255 || getTotalHomeworkScore() >= 720
                && getTotalTestScore() >= 285) {
            letterGrade = "G";
        } else {
            letterGrade = "A";
        }
    }

    public String getGrade() {
        return letterGrade;
    }

}

该类创建一个学生对象,其值可从文本文件中设置。下一个类创建了这些学生对象的数组,以及其他一些目前不重要的东西。现在重要的方法是 setStudents 方法,它创建学生对象数组:

public class CourseOffering {
    Student[] students;
    String description;
    double homeworkAverage;
    double testAverage;
    int passingStudents;

    public CourseOffering() {

    }

    public void setStudents(Student[] studentArray) {
        students = studentArray;
    }

    public void setDescription(String descript) {
        description = descript;

    }

    public String getDescription() {
        return description;
    }

    public double computeHomeworkAverage() {
        int temp = 0;
        for (int i = 0; i < students.length; i++) {
            temp += students[i].getTotalHomeworkScore();
        }
        homeworkAverage = temp / students.length;
        return homeworkAverage;
    }

    public double computeTestAverage() {
        int temp = 0;
        for (int j = 0; j < students.length; j++) {
            temp += students[j].getTotalTestScore();
        }

        testAverage = temp / students.length;
        return testAverage;
    }

    public int countPassingStudents() {
        int temp = 0;

        for (int k = 0; k < students.length; k++) {
            if (students[k].getGrade() == "G") {
                temp++;
            }
        }
        passingStudents = temp;
        return passingStudents;

    }

}

最后,这个类是运行整个事情的驱动程序:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class CourseStatistics {
    static int numberOfClasses = 0;
    static int numberOfStudents = 0;

    public static void main(String[] args) {
        Student myStudent = new Student();
        CourseOffering myCourseOffering = new CourseOffering();
        Scanner scanner = new Scanner(System.in);
        try {
            scanner = new Scanner(new File("gradesA5.txt"));
        } catch (FileNotFoundException e) {
            System.out
                    .println("Error opening file. Please make sure that you have a grades.txt file in the same folder as GradeCalculator.class");
            System.exit(0);
        }
        numberOfClasses = scanner.nextInt();
        System.out.println(numberOfClasses);

        myCourseOffering.setDescription(scanner.next()); // CSCE
        myCourseOffering.setDescription(scanner.next()); // 155A
        myCourseOffering.setDescription(scanner.next()); // -

        myCourseOffering.setDescription(scanner.next()); // Reads Semester
        System.out.print(myCourseOffering.getDescription() + " "); // Prints
                                                                    // Semester

        myCourseOffering.setDescription(scanner.next()); // Reads Year
        System.out.println(myCourseOffering.getDescription()); // Prints Year

        numberOfStudents = scanner.nextInt(); // Number Of Students
        System.out.println(numberOfStudents); // Prints number of students

        System.out.println("Name" + "\t" + "\t" + "Assignment Score" + "\t"
                + "Test Score" + "\t" + "Grade");

        myCourseOffering.students[0].setFirstName(scanner.next());
        System.out.println(myCourseOffering.students[0].getFirstName());

        // for (int i = 0; i < numberOfClasses; i++) {

        // }

    }
}

好的,现在是我遇到困难的部分。这段代码是我试图调用学生对象数组的第一个索引并通过读取文本文件设置 .firstName 值的地方:

myCourseOffering.students[0].setFirstName(scanner.next());
        System.out.println(myCourseOffering.students[0].getFirstName());

但我保留了空指针异常。异常指向 myCourseOffering.students[0].setFirstName(scanner.next());作为问题线,但我不确定问题是什么。这是我试图阅读的文本文件:

3
CSCE 155A - Fall 2011
4
Anthony Hopkins 80  90  95  87  80  78  25  17  20  22  21  24  19  22  21  23  24  21  20  25  20  55  56  110 30  20  25  8
John  Smith   99    95  82  72  64  52  15  14  11  21  25  12  19  20  21  23  21  12  12  10  15  50  50  60  25  15  20  9
Pan Mei     85  92  72  45  82  78  22  13  16  22  24  10  18  12  21  24  25  10  11  14  20  58  51  95  28  14  28  7
Rafael Vega    99   45  87  52  87  99  25  25  21  21  14  19  19  25  25  20  20  18  20  24  20  60  60  60  25  16  23  8
CSCE 155A - Spring 2012
1
Paul Kubi     80    90  5   87  80  0   25  0   20  22  21  24  19  22  21  0   24  21  20  25  20  0   0   0   30  20  25  8
CSCE 155A - Fall 2012
3
Tianna Delp   99    99  99  99  99  99  24  15  16  21  25  15  19  20  21  22  21  21  23  15  15  60  50  60  20  17  20  9
Taylor Delp   95    92  80  90  82  78  25  25  25  25  24  10  25  25  25  25  25  25  25  25  25  58  51  95  28  14  28  7
Rachel Valenz 99    45  87  52  87  99  25  25  21  21  14  19  19  25  25  20  20  18  20  24  20  60  60  60  25  16  23  8

【问题讨论】:

    标签: java arrays methods


    【解决方案1】:

    原因是当声明myCourseOffering

        CourseOffering myCourseOffering = new CourseOffering();
    

    您正在初始化 myCourseOffering 类对象,但成员 Student[] students 从未被初始化并保持为 null

    public class CourseOffering {
        Student[] students;          // never initialized, hence NULL
        String description;
        double homeworkAverage;
    

    因此初始化它(和其他成员以避免将来出现错误),最好在构造函数中

    public class CourseOffering {
        Student[] students;          
        String description;
        double homeworkAverage;
        double testAverage;
        int passingStudents;
    
        public CourseOffering() {
            this.students = new Student[100];          
            this.description = "";
            this.homeworkAverage = 0.0;
            this.testAverage = 0.0;
            this.passingStudents = 0;
    
        }
    

    或者你可以像这样在main方法中初始化它

    myCourseOffering.students = new Student[numberOfStudents];
    

    【讨论】:

      【解决方案2】:

      myCourseOffering.students 的值是 null,因为您还没有初始化数组。必须初始化 Java 中的数组,以及数组中的每个实例。

      使用new 和数组的特定大小来实例化一个数组 - 看起来上一行提供了学生人数,因此您需要阅读并使用它来实例化:

      myCourseOffering.students = new Student[10];
      

      此外,您可能需要初始化每个条目,可能是在某种循环中:

      for (int i = 0; i < number of students; i++) {
          myCourseOffering.students[i] = new Student();
          // populate properties of Student
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多