Home > Mobile >  Trying to pull data from *txt file to store into an array in main() to use as a data source for anot
Trying to pull data from *txt file to store into an array in main() to use as a data source for anot

Time:07-24

I have most of the code written down, and the input txt file ready to use to store data. While I run the program I get an error where the line I have the method and argument are highlighted.

public class Scores {
    int students;
    int tests;
    double[][] arrScores = new double[students][tests];

    Scores(int newStudent, int newTest) {
        students = newStudent;
        tests = newTest;
    }

    public void enterScore(int idxStudent, int idxTest, double fScore) {
        arrScores[idxStudent][idxTest] = fScore;
    }

    public void printScores() {
        System.out.printf("___________\tT# 0\tT# 1\tT# 2\tT# 3\tT# 4\n");
        for (int i = 0; i < 6; i  ) {
            for (int j = 0; j < 5; j  ) {
                System.out.printf("Student# %s\t", i);
                System.out.printf("%f\t", arrScores[i][j]);
            }
            System.out.printf("\n");
        }
    }

    public void printClassAvg() {
        double Class = 0;
        for (int i = 0; i < 6; i  ) {
            for (int j = 0; j < 5; j  ) {
                Class = Class   arrScores[i][j];
            }
            System.out.printf("Class Average: %.2f\n", Class);
        }
    }

    public void printStudentsAvg() {
        double average = 0;
        for (int i = 0; i < 6; i  ) {
            for (int j = 0; j < 5; j  ) {
                average = average   arrScores[i][j];
            }
            average = average / 5;
            System.out.printf("Student#%s Average: %.2f\n", average);
            average = 0;
        }
    }

    public void printStudentsAvgWithoutLowest() {
        double lowest = 100;
        double average = 10;
        for (int i = 0; i < 6; i  ) {
            for (int j = 0; j < 5; j  ) {
                if (lowest > arrScores[i][j]) {
                    lowest = arrScores[i][j];
                }
            }
            for (int k = 0; k < 5; k  ) {
                if (arrScores[i][k] != lowest) {
                    average = average   arrScores[i][k];
                }
            }
            average = average / 4;
            System.out.printf("Student# %s Average (without lowest score): %.2f", i, average);
            average = 0;
        }
    }

    public void printTestStats() {
        double average = 0;
        double stats = 0;
        double Class = 0;
        double[] arr = new double[5];
        for (int i = 0; i < 5; i  ) {
            for (int j = 0; j < 6; j  ) {
                average = average   arrScores[j][i];
                arr[j] = arrScores[j][i];
            }

            average = average / 5;
            for (double num = 0; num < arr.length; num  ) {
                stats = stats   Math.pow(num - average, 2);
            }
            Class = Math.sqrt(stats / 5);
            System.out.printf("Test#%s Average: %.2f with Std Deviation: %.2f", i, average, Class);
        }

    }
}

arrScores is where I receive the highlight and the program is terminated.

I am using Main class and another class to store all methods and calculations.

System.out.printf("%f\t", arrScores[i][j]); - this line im not getting value followed by the decimal values from the txt file. Example of numbers in txt file :

69.5

70.5

80.5

30.5

78.3

69.5

88.5

EDIT:


    public static void main(String[] args) throws FileNotFoundException {
        Scores score = new Scores(6,5);

        File input01 = new File("C:\\Users\\name\\Downloads\\input01.txt");
        Scanner data = new Scanner(input01);
        int students = 0;
        int tests = 0;
        int average;
        while (data.hasNextLine()) {
            average = (int) data.nextDouble();
            score.enterScore(students, tests, average);
            tests  ;
            if (tests == 5) {
                tests = 0;
                students  ;
            }
            if (students == 6) {
                break;
            }
        }
        score.printScores();
        score.printClassAvg();
        score.printStudentsAvg();
        score.printStudentsAvgWithoutLowest();
        score.printTestStats();

    }
}

CodePudding user response:

arrScores is where I receive the highlight and the program is terminated

Because when that line is executed, neither students nor tests have values. If you initialize students and tests in the constructor of class Scores, then you should also initialize arrScores in the same constructor – and not when you declare arrScores.

You also have an error in the following line of your code:

System.out.printf("Student#%s Average: %.2f\n", average);

You have two format specifiers, namely %s and %.2f but only one argument, namely average. The correction also appears in the below code.

Try the following code:

public class Scores {
    int students;
    int tests;
    double[][] arrScores; // do not initialize in declaration

    Scores(int newStudent, int newTest) {
        students = newStudent;
        tests = newTest;
        arrScores = new double[students][tests]; // initialize in constructor
    }

    public void enterScore(int idxStudent, int idxTest, double fScore) {
        arrScores[idxStudent][idxTest] = fScore;
    }

    public void printScores() {
        System.out.printf("___________\tT# 0\tT# 1\tT# 2\tT# 3\tT# 4\n");
        for (int i = 0; i < 6; i  ) {
            for (int j = 0; j < 5; j  ) {
                System.out.printf("Student# %s\t", i);
                System.out.printf("%f\t", arrScores[i][j]);
            }
            System.out.printf("\n");
        }
    }

    public void printClassAvg() {
        double Class = 0;
        for (int i = 0; i < 6; i  ) {
            for (int j = 0; j < 5; j  ) {
                Class = Class   arrScores[i][j];
            }
            System.out.printf("Class Average: %.2f\n", Class);
        }
    }

    public void printStudentsAvg() {
        double average = 0;
        for (int i = 0; i < 6; i  ) {
            for (int j = 0; j < 5; j  ) {
                average = average   arrScores[i][j];
            }
            average = average / 5;
            System.out.printf("Student#%s Average: %.2f\n", i, average); // CHANGE HERE
            average = 0;
        }
    }

    public void printStudentsAvgWithoutLowest() {
        double lowest = 100;
        double average = 10;
        for (int i = 0; i < 6; i  ) {
            for (int j = 0; j < 5; j  ) {
                if (lowest > arrScores[i][j]) {
                    lowest = arrScores[i][j];
                }
            }
            for (int k = 0; k < 5; k  ) {
                if (arrScores[i][k] != lowest) {
                    average = average   arrScores[i][k];
                }
            }
            average = average / 4;
            System.out.printf("Student# %s Average (without lowest score): %.2f", i, average);
            average = 0;
        }
    }

    public void printTestStats() {
        double average = 0;
        double stats = 0;
        double Class = 0;
        double[] arr = new double[5];
        for (int i = 0; i < 5; i  ) {
            for (int j = 0; j < 6; j  ) {
                average = average   arrScores[j][i];
                arr[j] = arrScores[j][i];
            }
            average = average / 5;
            for (double num = 0; num < arr.length; num  ) {
                stats = stats   Math.pow(num - average, 2);
            }
            Class = Math.sqrt(stats / 5);
            System.out.printf("Test#%s Average: %.2f with Std Deviation: %.2f", i, average, Class);
        }
    }
}
  • Related