0

I have my code working right, the only problem is that my output isn't correct because I can't find the right place to set the array of numbers back to zero. The basis of the programs is to take in data that contains names with corresponding grades. My output should follow this criteria:

        Alice [87, 99, 96, 99, 86, 96, 77, 95, 70, 88]
            Name:    Alice
            Length:  10
            Average: 89.30
            Median:  91.5
            Maximum: 99
            Mininum: 70

I get the first person's results correct, however, the ones that follow are incorrect because they contain all of the values being read in for every person. So, the next person will have "Alice's" grades plus their own when my code performs operations on the array for that person. I have two programs, the first is the main program that reads the data in and calls the methods to print and operate. The second is the class with all the methods that perform the operations. This is the main program:

    public class Lab2 {

public static void main(String[] args) {

    Scanner in = null; //initialize scanner
    ArrayList<Integer> gradeList = new ArrayList<Integer>(); //initialize gradeList

     //grab data from data.txt 
    try {
        in = new Scanner(new File("data.txt"));
    } catch (FileNotFoundException exception) {
        System.err.println("failed to open data.txt");
        System.exit(1);
    }
    //while loop to grab tokens from data
    while (in.hasNext()) {
        String studentName = in.next();   //name is the first token
        while (in.hasNextInt()) {   //while loop to grab all integer tokens after name
            int grade = in.nextInt();   //grade is next integer token
            gradeList.add(grade);       //adding every grade to gradeList
        }

      //grab all grades in gradeList and put them in an array to work with
        int[] sgrades = new int[gradeList.size()];
        for (int index = 0; index < gradeList.size(); index++) {
            sgrades[index] = gradeList.get(index);  //grade in gradeList put into grades array 
        }

        Grades myGrade = new Grades(studentName,sgrades);
        testGrades(myGrade);
        sgrades = null;


    }


}

public static void testGrades(Grades grades) {
    System.out.println(grades.toString()); 
    System.out.printf("\tName:    %s\n", grades.getName());
    System.out.printf("\tLength:  %d\n", grades.length());
    System.out.printf("\tAverage: %.2f\n", grades.average());
    System.out.printf("\tMedian:  %.1f\n", grades.median());
    System.out.printf("\tMaximum: %d\n", grades.maximum());
    System.out.printf("\tMininum: %d\n", grades.minimum());
    grades = null;
}

      }

I've tried adding place to erase the values of the array for the next person by setting it to null. I've had no luck with this.

This is the next program, which contains the methods.

    public class Grades {

private String studentName; // name of course this GradeBook represents
private int[] grades; // array of student grades

/**
 * @param studentName The name of the student.
 * @param grades The grades for the student.
 */

public Grades(String name, int[] sgrades) {
    studentName = name; // initialize courseName
    grades = sgrades; // store grades
} // end two-argument GradeBook constructor

/**
 * Method to convert array to a string and print.
 * 
 * @return The name of the student with array of grades.
 */
public String toString() {

    return (String) studentName + " " + Arrays.toString(grades);

}

/**
 * One-argument constructor initializes studentName.
 * The grades array is null.
 * 
 * @param name The name of the student.
 */

public Grades(String name) {
    studentName = name; // initialize courseName
} // end one-argument Grades constructor

/**
 * Method to set the student name.
 * 
 * @return The name of the student.
 */
public String getName() {
    return studentName;
} // end method getCourseName

/**
 * Method to set the length of the amount of grades.
 * 
 * @return Number of grades for student.
 */
public int length() {
    return grades.length; 
}

/**
 * Determine average grade for grades.
 * 
 * @return the average of the grades.
 */
public double average()     {      
    double total = 0; // initialize total
    double average = 0.0;
    // sum grades for one student, while loop
    int index = 0;
    while (index < grades.length) {
        int grade = grades[index];  // get grade at index
        total += grade;
        index++;                    // need to increment
    }
    average = total / grades.length;
    // return average of grades
    return (double) average;
} // end method getAverage


/**
 * Determine median grade for grades.
 * 
 * @return the median of the grades.
 */
public double median()      {

    Arrays.sort(grades);    //sort grades array
    double median = 0.0; 
    if (grades.length%2 == 0) //checks to see if amount of grades is even/odd
        //this is median if list of grades is even
        median = ((double)grades[grades.length/2-1] + (double)grades[grades.length/2])/2;
    else
        //this is median if list of grades is odd
        median = (double) grades[grades.length/2];

    return (double) median;
}

/**
 * Find minimum grade.
 * 
 * @return the minimum grade.
 */
public int minimum() { 
    int lowGrade = grades[0]; // assume grades[0] is smallest

    // loop through grades array, for loop
    for (int index = 0; index < grades.length; index++) {
        int grade = grades[index]; // get grade at index
        // if grade lower than lowGrade, assign it to lowGrade
        if (grade < lowGrade)
            lowGrade = grade; // new lowest grade
    } // end for

    return lowGrade; // return lowest grade
} // end method getMinimum

/**
 * Find maximum grade.
 * 
 * @return the maximum grade.
 */
public int maximum() { 
    int highGrade = grades[0]; // assume grades[0] is largest

    // loop through grades array, for-each loop
    for (int grade : grades) {
        // if grade greater than highGrade, assign it to highGrade
        if (grade > highGrade)
            highGrade = grade; // new highest grade
    } // end for

    return highGrade; // return highest grade
} // end method getMaximum

      }

My main question is, how can I "refresh" the array for every new student that I read in?

4

1 に答える 1

1

を探していgradeList.clear()ます。gradeListしかし、なぜ からにすべてをコピーするのsgradesですか? 冗長なようです。

于 2013-09-17T02:01:05.113 に答える