-1

我有一个名为 sectionArray 的二维数组,它是 Student 类的类型。每个学生都有一个姓名和一个包含 5 个等级的数组 [] 用于考试成绩。这些名称和分数被分成文件中必须读取的部分。无论我改变什么,我都会在我的 sectionArray 上得到一个 nullPointer。感谢您的任何建议。

import java.util.*;
import java.io.*;

public class ProgressReport {

    private Student[][] sectionArray;// each student has a name,
                                     // grade, average,
                                     // and the array of scores
    private File file;
    private Scanner inputFile;

    public ProgressReport() throws IOException {
        sectionArray = new Student[2][];
        file = new File("Lab5A.in.txt");
        inputFile = new Scanner(file);
    }

    /**
     * 
     * @return the values in the sectionArray
     */
    public Student[][] getSectionArray() {
        return sectionArray;
    }

    /**
     * initialize sectionArray and set the values
     * 
     * @param sectionArray
     *            passed 2D array of Students
     */
    public void setSectionArray(Student[][] sectionArray) {
        for (int i = 0; i < sectionArray.length; i++) {
            for (int j = 0; j < sectionArray[i].length; j++) {
                this.sectionArray[i][j] = sectionArray[i][j];
            }
        }
    }

    /**
     * reads from the file and creates new Students
     * 
     * @throws IOException
     */
    public void readInputFile() throws IOException {
        int colNum = 0;
        int section = 0;
        String name = " ";
        int[] grades = new int[5];

        while (inputFile.hasNext()) {
            colNum = inputFile.nextInt();// gets size of row
            sectionArray[section] = new Student[colNum];// initialize array

            // iterates through colNum amount of times
            for (int j = 0; j < colNum; j++) {
                name = inputFile.next();// gets next name in column
                for (int i = 0; i < 5; i++) {
                    // stores scores for that name
                    grades[i] = inputFile.nextInt();
                }
                // creates new Student with name and grades
                sectionArray[section][j] = new Student(name, grades);
                section++;
            }
        }

        // passes the values in sectionArray
        setSectionArray(sectionArray);
    }
}   

我的学生班是这样的:

public class Student {

    private String name = " "; // Store the name of the student
    private char grade; // Store the letter grade
    private double average; // Store the average score
    private int[] scores; // Store the five exam scores


    public Student() {
        grade = ' ';
        average = 0.0;
    }

    public Student(String name, int[] score) {
        this.name = name;
        scores = new int[5];
        for (int i = 0; i < scores.length; i++) {
            scores[i] = 0;
        }
        for (int i = 0; i < scores.length; i++) {
            this.scores[i] = score[i];
        }
    }

    // getters

    public String getName() {
        return name;
    }

    public char getGrade() {
        return grade;
    }

    public double getAverage() {
        return average;
    }

    // think about changing this to return a different format
    public int[] getScores() {
        return scores;
    }

    // setters
    public void setScore(int[] scores) {

    }

    public void setName(String name) {
        this.name = name;
    }

    public void setGrade(char grade) {
        this.grade = grade;
    }

    public void setAverage(double average) {
        this.average = average;
    }

    /**
     * determine the average of the five test scores for each student
     */
    public void calculateAverage() {
        double total = 0;
        double average = 0;
        for (int i = 0; i < scores.length; i++) {
            total += scores[i];
        }
        average = total / scores.length;
        setAverage(average);
    }

    /**
     * Determine the student's letter grade based on average of test scores
     */
    public void calculateGrade() {
        double average = 0;
        average = getAverage();
        if (average <= 100 && average >= 90) {
            setGrade('A');
        } else if (average <= 89 && average >= 80) {
            setGrade('B');
        } else if (average <= 79 && average >= 70) {
            setGrade('C');
        } else if (average <= 69 && average >= 60) {
            setGrade('D');
        } else if (average <= 59 && average >= 0) {
            setGrade('F');
        }

    }

    public String toString() {
        return getName() + " " + getAverage() + " " + getGrade();
    }
}
4

1 回答 1

2

您尝试写入的数组是用 初始化的sectionArray = new Student[2][];。这将创建一个包含 2 列且只有一行的矩阵(2D 数组),然后您尝试在该数组上设置新值。如果您已经知道要从文件中读取的矩阵的大小,请使用正确的值对其进行初始化。

无论如何,我不明白您为什么要为此尝试使用二维数组。如果我正确理解了您的代码的目的,您应该使用 aList来存储读取的数据,并且由于它具有动态增加的大小,您不必像使用数组那样控制索引。查看本教程以了解如何使用列表。

于 2013-10-14T16:30:01.007 回答