简体   繁体   中英

Java: Finding average of doubles in array

I'm working on an assignment for my intro to java class. Part of the assignment is reading in doubles from a text file into an array and then using a different method to calculate the average. So far I've read the doubles into the array but I'm not sure how to reference that array in another method to calculate the average. I've been searching online, but with no results probably because my poor terminology leads me to irrelevant answers. I get an error from netbeans that the symbol classGrades can't be found(because its only in the main method.) I tried defininig it as a global variable, but that didn't work and we haven't learned about those yet so there must be another way to do it.

To summarize: I don't know how to reference variables between methods.

Here is my code

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

public class GradeChecker {

public static void main(String[] args) {
    // TODO code application logic here
    double[] classGrades = new double[76];
    double classAverage = calculateAverageGrade();


    fillArray(classGrades);
    for (int i = 0; i < classGrades.length; i++) {
        System.out.println(classGrades[i]);
    }
    //System.out.print(Arrays.toString(classGrades));
    System.out.print(classGrades.length);

}
    public static void fillArray(double[] ary) {
    try {
        File arrayInput = new File("ClassGrades.txt");
        Scanner in = new Scanner(arrayInput);
        in.useDelimiter("\r\n");

        int i = 0;
        while (in.hasNextLine()) {
            ary[i++] = in.nextDouble();
        }
        in.close();
    } catch (Exception exception) {
        System.out.println("Error: " + exception.getMessage());
    }
}

    private static double calculateAverageGrade(double[] classGrades) {
    double gradeSum = 0;
    double gradeAverage = 0;
    for (int i = 0.0; i < classGrades.length; i++) {
        gradeSum = gradeSum + classGrades[i];
    }
    gradeAverage = gradeSum/classGrades.length;
    return gradeAverage;
}
}

You can pass array classGrades to the function:

    double[] classGrades = new double[76];
    double classAverage = calculateAverageGrade(classGrades);

Here:

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

public class GradeChecker {

    public static void main(String[] args) {
        // TODO code application logic here
        double[] classGrades = new double[76];
        double classAverage = calculateAverageGrade(classGrades);


        fillArray(classGrades);
        for (int i = 0; i < classGrades.length; i++) {
            System.out.println(classGrades[i]);
        }
        //System.out.print(Arrays.toString(classGrades));
        System.out.print(classGrades.length);

    }

    public static void fillArray(double[] ary) {
        try {
            File arrayInput = new File("ClassGrades.txt");
            Scanner in = new Scanner(arrayInput);
            in.useDelimiter("\r\n");

            int i = 0;
            while (in.hasNextLine()) {
                ary[i++] = in.nextDouble();
            }
            in.close();
        } catch (Exception exception) {
            System.out.println("Error: " + exception.getMessage());
        }
    }

    private static double calculateAverageGrade(double[] classGrades) {
        double gradeSum = 0;
        double gradeAverage = 0;
        for (int i = 0; i < classGrades.length; i++) {
            gradeSum = gradeSum + classGrades[i];
        }
        gradeAverage = gradeSum / classGrades.length;
        return gradeAverage;
    }
}

I have been edited your code.

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

public class GradeChecker {

    public static void main(String[] args) {
        // TODO code application logic here
        double[] classGrades = new double[76];
        fillArray(classGrades);

        double classAverage = calculateAverageGrade(classGrades);

        for (int i = 0; i < classGrades.length; i++) {
            System.out.println(classGrades[i]);
        }
        //System.out.print(Arrays.toString(classGrades));
        System.out.print(classGrades.length);

    }
    public static void fillArray(double[] ary) {
        try {
            File arrayInput = new File("ClassGrades.txt");
            Scanner in = new Scanner(arrayInput);
            in.useDelimiter("\r\n");

            int i = 0;
            while (in.hasNextLine()) {
                ary[i++] = in.nextDouble();
            }
            in.close();
        } catch (Exception exception) {
            System.out.println("Error: " + exception.getMessage());
        }
    }

    private static double calculateAverageGrade(double[] classGrades) {
        double gradeSum = 0;
        double gradeAverage = 0;
        for (int i = 0; i < classGrades.length; i++) {
            gradeSum = gradeSum + classGrades[i];
        }
        gradeAverage = gradeSum/classGrades.length;
        return gradeAverage;
    }
}

Please try this and let me know

EDIT 1

  • Lot of compilation issue.
  • fillArray called after finding calculateAverageGrade

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM