-1

I have a gradebook program that uses 2 dimensional arrays to read in an infile with 2 exam grades and then the name of the students.the program is

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

public class grades {

    public static int numberOfStudents;
    public static int numberOfExams;

    public static void main(String[] args) throws IOException {
        String[] Students;
        Students = new String[25]; // the program allows for up to 25 students
        int[][] Grades;
        Grades = new int[25][10]; // and 10 exam grades per student
        BufferedReader inFile;
        inFile = new BufferedReader(new FileReader("Gradebook.txt"));

        int option;
        boolean done = false;
        numberOfStudents = Integer.parseInt(inFile.readLine());
        numberOfExams = Integer.parseInt(inFile.readLine());
        loadArrays(Students, Grades, inFile, numberOfStudents, numberOfExams);

        do {
            option = pickOption();
            done = selectFunction(Students, Grades, option);
        }
        while (!done);
        cleanUp(Students, Grades, numberOfStudents, numberOfExams);
    }

    public static void loadArrays(String[] Students, int[][] Grades, BufferedReader inFile, int numberOfStudents,
                                  int numberOfExams) throws IOException {
        for (int row = 0; row < numberOfStudents; row++) {
            Students[row] = inFile.readLine();
            for (int col = 0; col < numberOfExams; col++) {
                Grades[row][col] = Integer.parseInt(inFile.readLine());
            }
        }
    }

    public static void printWholeBook(String[] Students, int[][] Grades, int numberOfStudents, int numberOfExams) {
        System.out.println("Here are the grades for all students.\n");
        System.out.print("Name");
        for (int cnt = 1; cnt <= numberOfExams; cnt++)
            System.out.print("  Exam" + cnt);
        System.out.println();
        for (int row = 0; row < numberOfStudents; row++) {
            System.out.print(Students[row] + "   ");

            for (int col = 0; col < numberOfExams; col++) {
                System.out.print(Grades[row][col] + "     ");
            }
            System.out.println();
        }
    }

    public static int pickOption() throws IOException {
        BufferedReader in;
        in = new BufferedReader(new InputStreamReader(System.in));
        int choice;

        System.out.println("\nWhat would you like to do?");
        System.out.println("1) Compute the average for all students?");
        System.out.println("2) Compute the average for all exams?");
        System.out.println("3) Add a new student?");
        System.out.println("4) Add a new exam?");
        System.out.println("5) Print the whole grade book?");
        System.out.println("6) Compute the average for one student and their letter grade?");
        System.out.println("7) Compute the average for one exam and its letter grade?");
        System.out.println("9) Exit the program?\n");
        choice = Integer.parseInt(in.readLine());
        return choice;
    }

    public static boolean selectFunction(String[] Students, int[][] Grades,
                                         int option) throws IOException {  // Class variables int numberOfStudents and int numberOfExams are not passed as params
//  to this method because they are potentially modified here
// and stupid JAVA will only allow you to return one value
// also, class variable do no need to be passed but show-up light blue
// so we know that they are class variables
        boolean done = false;
        switch (option) {
            case 1: {
                allStudentAverage(Students, Grades, numberOfStudents, numberOfExams);
                break;
            }

            case 2: {
                allExamAverage(Students, Grades, numberOfStudents, numberOfExams);
                break;
            }
            case 3: {
                numberOfStudents++;
                addStudent(Students, Grades, numberOfStudents, numberOfExams);
                break;
            }
            case 4: {
                numberOfExams++;
                addExam(Students, Grades, numberOfStudents, numberOfExams);
                break;
            }
            case 5: {
                printWholeBook(Students, Grades, numberOfStudents, numberOfExams);
                break;
            }
            case 6: {
                oneStudentAverage(Students, Grades, numberOfStudents, numberOfExams);
                break;
            }
            case 7: {
                oneExamAverage(Students, Grades, numberOfStudents, numberOfExams);
                break;
            }
            case 9: {
                done = true;
                break;
            }
            default:
                System.out.println("Invalid input.");
        } // end switch
        return done;
    } // end select function

    public static void allStudentAverage(String[] Students, int[][] Grades,
                                         int numberOfStudents, int numberOfExams) {
        double total = 0;
        System.out.println("Here are the averages for all students.\n");
        for (int cnt1 = 0; cnt1 < numberOfStudents; cnt1++) {
            System.out.print("The average for " + Students[cnt1] + " is ");
            for (int cnt2 = 0; cnt2 < numberOfExams; cnt2++) {
                total = total + Grades[cnt1][cnt2];
            }
            System.out.println(total / numberOfExams);
            total = 0;
        }
    }

    public static void allExamAverage(String[] Students, int[][] Grades, int numberOfStudents,
                                      int numberOfExams) {
        double total = 0;
        System.out.println("Here are the averages for all exams.\n");
        for (int cnt1 = 0; cnt1 < numberOfExams; cnt1++) {
            System.out.print("The average for Exam #" + (cnt1 + 1) + " is ");
            for (int cnt2 = 0; cnt2 < numberOfStudents; cnt2++) {
                total = total + Grades[cnt2][cnt1];
            }
            System.out.println(total / numberOfStudents);
            total = 0;
        }
    }

    public static void addStudent(String[] Students, int[][] Grades, int numberOfStudents,
                                  int numberOfExams) throws IOException {
        BufferedReader in;
        in = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the name of the student to be added.");
        Students[numberOfStudents - 1] = in.readLine();
        System.out.println("Enter the grades for the new student.");
        for (int cnt = 0; cnt <= numberOfExams - 1; cnt++) {
            System.out.println("Enter grade for exam " + (cnt + 1));
            Grades[numberOfStudents - 1][cnt] = Integer.parseInt(in.readLine());
        }
    }

    public static void addExam(String[] Students, int[][] Grades, int numberOfStudents,
                               int numberOfExams) throws IOException {
        BufferedReader in;
        in = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter grades for the new exam.");
        for (int cnt = 0; cnt <= numberOfStudents - 1; cnt++) {
            System.out.println("Enter the new grade for " + Students[cnt]);
            Grades[cnt][numberOfExams - 1] = Integer.parseInt(in.readLine());
        }
    }

    public static void oneStudentAverage(String[] Students, int[][] Grades,
                                         int numberOfStudents, int numberOfExams) throws IOException {
        BufferedReader in;
        in = new BufferedReader(new InputStreamReader(System.in));
        double total = 0;
        int studentNumber;
        System.out.println("Please enter the number of the student whose average would you like to see?\n");
        for (int cnt = 0; cnt < numberOfStudents; cnt++) {
            System.out.println((cnt + 1) + ") " + Students[cnt]);
        }
        studentNumber = Integer.parseInt(in.readLine());
        System.out.print("\nThe average for " + Students[studentNumber - 1] + " is ");
        for (int cnt2 = 0; cnt2 < numberOfExams; cnt2++) {
            total = total + Grades[studentNumber - 1][cnt2];
        }
        double average = total / numberOfExams;
        System.out.println(average);

        if (average >= 90) {
            System.out.println(Students[studentNumber - 1] + " has an A.");
        } else if (average >= 80 && average <= 89) {
            System.out.println(Students[studentNumber - 1] + " has a B.");
        } else if (average >= 70 && average <= 79) {
            System.out.println(Students[studentNumber - 1] + " has a C.");
        } else if (average >= 60 && average <= 69) {
            System.out.println(Students[studentNumber - 1] + " has a D.");
        } else if (average <= 59) {
            System.out.println(Students[studentNumber - 1] + " has an F.");
        }
    }

    public static void oneExamAverage(String[] Students, int[][] Grades, int numberOfStudents,
                                      int numberOfExams) throws IOException {
        BufferedReader in;
        in = new BufferedReader(new InputStreamReader(System.in));
        double total = 0;
        int examNumber;
        System.out.println("There are " + numberOfExams + " exams.");
        System.out.println("Please enter the number of the exam whose average would you like to see?\n");
        examNumber = Integer.parseInt(in.readLine());
        System.out.print("The average for Exam #" + (examNumber) + " is ");
        for (int cnt2 = 0; cnt2 < numberOfStudents; cnt2++) {
            total = total + Grades[cnt2][examNumber - 1];
        }
        double average = total / numberOfStudents;
        System.out.println(average);

        if (average >= 90) {
            System.out.println("The average for Exam #" + examNumber + " is an A.");
        } else if (average >= 80 && average <= 89) {
            System.out.println("The average for Exam #" + examNumber + " is a B.");
        } else if (average >= 70 && average <= 79) {
            System.out.println("The average for Exam #" + examNumber + " is a C.");
        } else if (average >= 60 && average <= 69) {
            System.out.println("The average for Exam #" + examNumber + " is a D.");
        } else if (average <= 59) {
            System.out.println("The average for Exam # " + examNumber + " is an F.");
        }
    }

    public static void cleanUp(String[] Students, int[][] Grades, int numberOfStudents,
                               int numberOfExams) throws IOException {
// set up output file
        PrintWriter outFile;
        outFile = new PrintWriter(new FileWriter("Gradebook.txt"));
        outFile.println(numberOfStudents);
        outFile.println(numberOfExams);
        // write array to output file
        for (int row = 0; row < numberOfStudents; row++) {
            outFile.println(Students[row]);
            for (int col = 0; col < numberOfExams; col++) {
                outFile.println(Grades[row][col]);
            }
        }
        outFile.close();
        System.out.println("The Grade Book file is saved so you can");
        System.out.println("pick up next time where you left off.\n");
    }
}

the infile is

5
2
Adams
75
75
Bush 
65
75
Cosby
99
99
Duke 
90
80
East 
50
50

For an assignment, I was tasked with instead of using arrays, I have to use array list. I changed Students and Grades into 2 lists, but I'm encountering a bunch of errors that I couldn't quite figure out. This is my attempt at using array list so far

import java.io.*;
import java.util.*;
import java.util.List;
import java.util.ArrayList;


public class gradebookarraylist {

    public static int numberOfStudents;
    public static int numberOfExams;

    public static void main(String[] args) throws IOException {
        ArrayList<String> Students = new ArrayList<String>(); // the program allows for up to 25 students -> change to array list type str hold students names
        ArrayList<Integer> Grades = new ArrayList<Integer>(); // and 10 exam grades per student-> change to array list hold student grades type int

        BufferedReader inFile;
        inFile = new BufferedReader(new FileReader("Gradebook.txt"));

        int option;
        boolean done = false;
        numberOfStudents = Integer.parseInt(inFile.readLine());
        numberOfExams = Integer.parseInt(inFile.readLine());
        loadArrays(Students, Grades, inFile, numberOfStudents, numberOfExams);

        do {
            option = pickOption();
            done = selectFunction(Students, Grades, option);
        }
        while (!done);
        cleanUp(Students, Grades, numberOfStudents, numberOfExams);
    }

    public static void loadArrays(ArrayList<String> students, ArrayList<Integer> grades, BufferedReader inFile, int numberOfStudents,
                                  int numberOfExams) throws IOException {
        for (int row = 0; row < numberOfStudents; row++) {
            String Students = inFile.readLine();
            for (int col = 0; col < numberOfExams; col++) {
                int Grades = Integer.parseInt(inFile.readLine());
            }
        }
    }

    public static void printWholeBook(ArrayList<String> students, ArrayList<Integer> grades, int numberOfStudents, int numberOfExams) {
        System.out.println("Here are the grades for all students.\n");
        System.out.print("Name");
        for (int cnt = 1; cnt <= numberOfExams; cnt++)
            System.out.print("  Exam" + cnt);
        System.out.println();
        for (int i = 0; i < numberOfStudents; i++) {
            System.out.print(students.get(i) + "   ");

            for (int j = 0; j < numberOfExams; j++) {
                System.out.print(grades.get(j) + "     ");
            }
            System.out.println();
        }
    }

    public static int pickOption() throws IOException {
        BufferedReader in;
        in = new BufferedReader(new InputStreamReader(System.in));
        int choice;

        System.out.println("\nWhat would you like to do?");
        System.out.println("1) Compute the average for all students?");
        System.out.println("2) Compute the average for all exams?");
        System.out.println("3) Add a new student?");
        System.out.println("4) Add a new exam?");
        System.out.println("5) Print the whole grade book?");
        System.out.println("6) Compute the average for one student and their letter grade?");
        System.out.println("7) Compute the average for one exam and its letter grade?");
        System.out.println("9) Exit the program?\n");
        choice = Integer.parseInt(in.readLine());
        return choice;
    }

    public static boolean selectFunction(ArrayList<String> students, ArrayList<Integer> grades, int option) throws IOException {
        boolean done = false;
        switch (option) {
            case 1: {
                allStudentAverage(students, grades, numberOfStudents, numberOfExams);
                break;
            }

            case 2: {
                allExamAverage(students, grades, numberOfStudents, numberOfExams);
                break;
            }
            case 3: {
                numberOfStudents++;
                addStudent(students, grades, numberOfStudents, numberOfExams);
                break;
            }
            case 4: {
                numberOfExams++;
                addExam(students, grades, numberOfStudents, numberOfExams);
                break;
            }
            case 5: {
                printWholeBook(students, grades, numberOfStudents, numberOfExams);
                break;
            }
            case 6: {
                oneStudentAverage(students, grades, numberOfStudents, numberOfExams);
                break;
            }
            case 7: {
                oneExamAverage(students, grades, numberOfStudents, numberOfExams);
                break;
            }
            case 9: {
                done = true;
                break;
            }
            default:
                System.out.println("Invalid input.");
        } // end switch
        return done;
    } // end select function


    public static void allStudentAverage(ArrayList<String> students, ArrayList<Integer> grades,
                                         int numberOfStudents, int numberOfExams) {
        double total = 0;
        System.out.println("Here are the averages for all students.\n");
        for (int cnt1 = 0; cnt1 < numberOfStudents; cnt1++) {
            System.out.print("The average for " + students[cnt1] + " is ");
            for (int cnt2 = 0; cnt2 < numberOfExams; cnt2++) {
                total = total + grades[cnt1][cnt2];
            }
            System.out.println(total / numberOfExams);
            total = 0;
        }
    }

    public static void allExamAverage(ArrayList<String> students, ArrayList<Integer> grades, int numberOfStudents,
                                      int numberOfExams) {
        double total = 0;
        System.out.println("Here are the averages for all exams.\n");
        for (int cnt1 = 0; cnt1 < numberOfExams; cnt1++) {
            System.out.print("The average for Exam #" + (cnt1 + 1) + " is ");
            for (int cnt2 = 0; cnt2 < numberOfStudents; cnt2++) {
                total = total + grades[cnt2][cnt1];
            }
            System.out.println(total / numberOfStudents);
            total = 0;
        }
    }

    public static void addStudent(ArrayList<String> students, ArrayList<Integer> grades, int numberOfStudents,
                                  int numberOfExams) throws IOException {
        BufferedReader in;
        in = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the name of the student to be added.");
        students.add(in.readLine());
        System.out.println("Enter the grades for the new student.");
        for (int cnt = 0; cnt <= numberOfExams - 1; cnt++) {
            System.out.println("Enter grade for exam " + (cnt + 1));
            grades.add(Integer.parseInt(in.readLine()));
        }
    }

    public static void addExam(ArrayList<String> students, ArrayList<Integer> grades, int numberOfStudents,
                               int numberOfExams) throws IOException {
        BufferedReader in;
        in = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter grades for the new exam.");
        for (int cnt = 0; cnt <= numberOfStudents - 1; cnt++) {
            System.out.println("Enter the new grade for " + students[cnt]);
            grades.add(Integer.parseInt(in.readLine()));
        }
    }

    public static void oneStudentAverage(ArrayList<String> students, ArrayList<Integer> grades,
                                         int numberOfStudents, int numberOfExams) throws IOException {
        BufferedReader in;
        in = new BufferedReader(new InputStreamReader(System.in));
        double total = 0;
        int studentNumber;
        System.out.println("Please enter the number of the student whose average would you like to see?\n");
        for (int cnt = 0; cnt < numberOfStudents; cnt++) {
            System.out.println((cnt + 1) + ") " + students[cnt]);
        }
        studentNumber = Integer.parseInt(in.readLine());
        System.out.print("\nThe average for " + students[studentNumber - 1] + " is ");
        for (int cnt2 = 0; cnt2 < numberOfExams; cnt2++) {
            total = total + grades[studentNumber - 1][cnt2];
        }
        double average = total / numberOfExams;
        System.out.println(average);

        if (average >= 90) {
            System.out.println(students[studentNumber - 1] + " has an A.");
        } else if (average >= 80 && average <= 89) {
            System.out.println(students[studentNumber - 1] + " has a B.");
        } else if (average >= 70 && average <= 79) {
            System.out.println(students[studentNumber - 1] + " has a C.");
        } else if (average >= 60 && average <= 69) {
            System.out.println(students[studentNumber - 1] + " has a D.");
        } else if (average <= 59) {
            System.out.println(students[studentNumber - 1] + " has an F.");
        }
    }

    public static void oneExamAverage(ArrayList<String> students, ArrayList<Integer> grades, int numberOfStudents,
                                      int numberOfExams) throws IOException {
        BufferedReader in;
        in = new BufferedReader(new InputStreamReader(System.in));
        double total = 0;
        int examNumber;
        System.out.println("There are " + numberOfExams + " exams.");
        System.out.println("Please enter the number of the exam whose average would you like to see?\n");
        examNumber = Integer.parseInt(in.readLine());
        System.out.print("The average for Exam #" + (examNumber) + " is ");
        for (int cnt2 = 0; cnt2 < numberOfStudents; cnt2++) {
            total = total + grades[cnt2][examNumber - 1];
        }
        double average = total / numberOfStudents;
        System.out.println(average);

        if (average >= 90) {
            System.out.println("The average for Exam #" + examNumber + " is an A.");
        } else if (average >= 80 && average <= 89) {
            System.out.println("The average for Exam #" + examNumber + " is a B.");
        } else if (average >= 70 && average <= 79) {
            System.out.println("The average for Exam #" + examNumber + " is a C.");
        } else if (average >= 60 && average <= 69) {
            System.out.println("The average for Exam #" + examNumber + " is a D.");
        } else if (average <= 59) {
            System.out.println("The average for Exam # " + examNumber + " is an F.");
        }
    }

    public static void cleanUp(ArrayList<String> students, ArrayList<Integer> grades, int numberOfStudents,
                               int numberOfExams) throws IOException {
// set up output file
        PrintWriter outFile;
        outFile = new PrintWriter(new FileWriter("Gradebook.txt"));
        outFile.println(numberOfStudents);
        outFile.println(numberOfExams);
        // write array to output file
        for (int row = 0; row < numberOfStudents; row++) {
            outFile.println(students.get(row));
            for (int col = 0; col < numberOfExams; col++) {
                outFile.println(grades.get(col));
            }
        }
        outFile.close();
        System.out.println("The Grade Book file is saved so you can");
        System.out.println("pick up next time where you left off.\n");
    }
}

Any help is appreciated.

3
  • 3
    What errors are you getting, what code is causing them, and please, please learn to indent properly. Commented Feb 24, 2020 at 21:47
  • 2
    I have to say....there can't be anything more annoying than trying to look through code that isn't properly indented. Just sayin. There this SO Post or this other SO Post. Commented Feb 24, 2020 at 21:56
  • 1
    @midnightblue look at the error carefully while compiling second program. It's pretty self explanatory. study ArrayList class usage. then you'll know how to access arrayList item using get(int index) method instead of [] operator. Commented Feb 25, 2020 at 4:17

4 Answers 4

0

I will give you the following example

String [] names = {"John","Bill","Sam"};
List arrylist =Arrays.asList(names);

That will convert your array to an array list, if that was the answer you were searching for.

Sign up to request clarification or add additional context in comments.

Comments

0

The errors you are getting are because the ArrayList syntax is different. I suggest you comment out all the old code that doesn't compile and start fixing it method by method. Use the "change a little, test a little" approach.

Here's a start:

I have renamed your Students and Grades variables in loadArrays() since they are singular items, not plural. Java convention suggests using lowercase names for variables. Also, use upper camel case for your class names: GradeBookArrayList.

public static void loadArrays(ArrayList<String> students, ArrayList<Integer> grades, BufferedReader inFile, int numberOfStudents,
                              int numberOfExams) throws IOException {
    for (int row = 0; row < numberOfStudents; row++) {
        String student = inFile.readLine();
        students.add(student); // How to add to students array
        for (int col = 0; col < numberOfExams; col++) {
            int grade = Integer.parseInt(inFile.readLine());
            grades.add(grade); // How to add to grades array
        }
    }
}

The syntax for ArrayList doesn't use square brackets for indexing like primitive arrays do. Please refer to the online documentation or examples of ArrayList syntax.

Here is your commented, compilable and indented code:

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


public class gradebookarraylist { // Use upper camel case for your class names: GradeBookArrayList

    public static int numberOfStudents;
    public static int numberOfExams;

    public static void main(String[] args) throws IOException {
        ArrayList<String> Students = new ArrayList<String>(); // the program allows for up to 25 students -> change to array list type str hold students names
        ArrayList<Integer> Grades = new ArrayList<Integer>(); // and 10 exam grades per student-> change to array list hold student grades type int

        BufferedReader inFile;
        inFile = new BufferedReader(new FileReader("Gradebook.txt"));

        int option;
        boolean done = false;
        numberOfStudents = Integer.parseInt(inFile.readLine());
        numberOfExams = Integer.parseInt(inFile.readLine());
        loadArrays(Students, Grades, inFile, numberOfStudents, numberOfExams);

        do {
            option = pickOption();
            done = selectFunction(Students, Grades, option);
        }
        while (!done);
        cleanUp(Students, Grades, numberOfStudents, numberOfExams);
    }

    public static void loadArrays(ArrayList<String> students, ArrayList<Integer> grades, BufferedReader inFile, int numberOfStudents,
                                  int numberOfExams) throws IOException {
        for (int row = 0; row < numberOfStudents; row++) {
            String student = inFile.readLine();
            students.add(student);
            for (int col = 0; col < numberOfExams; col++) {
                int grade = Integer.parseInt(inFile.readLine());
                grades.add(grade);
            }
        }
    }

    public static void printWholeBook(ArrayList<String> students, ArrayList<Integer> grades, int numberOfStudents, int numberOfExams) {
        System.out.println("Here are the grades for all students.\n");
        System.out.print("Name");
        for (int cnt = 1; cnt <= numberOfExams; cnt++)
            System.out.print("  Exam" + cnt);
        System.out.println();
        for (int i = 0; i < numberOfStudents; i++) {
            System.out.print(students.get(i) + "   ");

            for (int j = 0; j < numberOfExams; j++) {
                System.out.print(grades.get(j) + "     ");
            }
            System.out.println();
        }
    }

    public static int pickOption() throws IOException {
        BufferedReader in;
        in = new BufferedReader(new InputStreamReader(System.in));
        int choice;

        System.out.println("\nWhat would you like to do?");
        System.out.println("1) Compute the average for all students?");
        System.out.println("2) Compute the average for all exams?");
        System.out.println("3) Add a new student?");
        System.out.println("4) Add a new exam?");
        System.out.println("5) Print the whole grade book?");
        System.out.println("6) Compute the average for one student and their letter grade?");
        System.out.println("7) Compute the average for one exam and its letter grade?");
        System.out.println("9) Exit the program?\n");
        choice = Integer.parseInt(in.readLine());
        return choice;
    }

    public static boolean selectFunction(ArrayList<String> students, ArrayList<Integer> grades, int option) throws IOException {
        boolean done = false;
        switch (option) {
            case 1: {
                allStudentAverage(students, grades, numberOfStudents, numberOfExams);
                break;
            }

            case 2: {
                allExamAverage(students, grades, numberOfStudents, numberOfExams);
                break;
            }
            case 3: {
                numberOfStudents++;
                addStudent(students, grades, numberOfStudents, numberOfExams);
                break;
            }
            case 4: {
                numberOfExams++;
                addExam(students, grades, numberOfStudents, numberOfExams);
                break;
            }
            case 5: {
                printWholeBook(students, grades, numberOfStudents, numberOfExams);
                break;
            }
            case 6: {
                oneStudentAverage(students, grades, numberOfStudents, numberOfExams);
                break;
            }
            case 7: {
                oneExamAverage(students, grades, numberOfStudents, numberOfExams);
                break;
            }
            case 9: {
                done = true;
                break;
            }
            default:
                System.out.println("Invalid input.");
        } // end switch
        return done;
    } // end select function


    public static void allStudentAverage(ArrayList<String> students, ArrayList<Integer> grades,
                                         int numberOfStudents, int numberOfExams) {
        double total = 0;
        System.out.println("Here are the averages for all students.\n");
//        for (int cnt1 = 0; cnt1 < numberOfStudents; cnt1++) {
//            System.out.print("The average for " + students[cnt1] + " is ");
//            for (int cnt2 = 0; cnt2 < numberOfExams; cnt2++) {
//                total = total + grades[cnt1][cnt2];
//            }
//            System.out.println(total / numberOfExams);
//            total = 0;
//        }
    }

    public static void allExamAverage(ArrayList<String> students, ArrayList<Integer> grades, int numberOfStudents,
                                      int numberOfExams) {
        double total = 0;
        System.out.println("Here are the averages for all exams.\n");
//        for (int cnt1 = 0; cnt1 < numberOfExams; cnt1++) {
//            System.out.print("The average for Exam #" + (cnt1 + 1) + " is ");
//            for (int cnt2 = 0; cnt2 < numberOfStudents; cnt2++) {
//                total = total + grades[cnt2][cnt1];
//            }
//            System.out.println(total / numberOfStudents);
//            total = 0;
//        }
    }

    public static void addStudent(ArrayList<String> students, ArrayList<Integer> grades, int numberOfStudents,
                                  int numberOfExams) throws IOException {
        BufferedReader in;
        in = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the name of the student to be added.");
        students.add(in.readLine());
        System.out.println("Enter the grades for the new student.");
        for (int cnt = 0; cnt <= numberOfExams - 1; cnt++) {
            System.out.println("Enter grade for exam " + (cnt + 1));
            grades.add(Integer.parseInt(in.readLine()));
        }
    }

    public static void addExam(ArrayList<String> students, ArrayList<Integer> grades, int numberOfStudents,
                               int numberOfExams) throws IOException {
        BufferedReader in;
        in = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter grades for the new exam.");
//        for (int cnt = 0; cnt <= numberOfStudents - 1; cnt++) {
//            System.out.println("Enter the new grade for " + students[cnt]);
//            grades.add(Integer.parseInt(in.readLine()));
//        }
    }

    public static void oneStudentAverage(ArrayList<String> students, ArrayList<Integer> grades,
                                         int numberOfStudents, int numberOfExams) throws IOException {
        BufferedReader in;
        in = new BufferedReader(new InputStreamReader(System.in));
        double total = 0;
        int studentNumber;
        System.out.println("Please enter the number of the student whose average would you like to see?\n");
//        for (int cnt = 0; cnt < numberOfStudents; cnt++) {
//            System.out.println((cnt + 1) + ") " + students[cnt]);
//        }
        studentNumber = Integer.parseInt(in.readLine());
//        System.out.print("\nThe average for " + students[studentNumber - 1] + " is ");
//        for (int cnt2 = 0; cnt2 < numberOfExams; cnt2++) {
//            total = total + grades[studentNumber - 1][cnt2];
//        }
        double average = total / numberOfExams;
        System.out.println(average);

//        if (average >= 90) {
//            System.out.println(students[studentNumber - 1] + " has an A.");
//        } else if (average >= 80 && average <= 89) {
//            System.out.println(students[studentNumber - 1] + " has a B.");
//        } else if (average >= 70 && average <= 79) {
//            System.out.println(students[studentNumber - 1] + " has a C.");
//        } else if (average >= 60 && average <= 69) {
//            System.out.println(students[studentNumber - 1] + " has a D.");
//        } else if (average <= 59) {
//            System.out.println(students[studentNumber - 1] + " has an F.");
//        }
    }

    public static void oneExamAverage(ArrayList<String> students, ArrayList<Integer> grades, int numberOfStudents,
                                      int numberOfExams) throws IOException {
        BufferedReader in;
        in = new BufferedReader(new InputStreamReader(System.in));
        double total = 0;
        int examNumber;
        System.out.println("There are " + numberOfExams + " exams.");
        System.out.println("Please enter the number of the exam whose average would you like to see?\n");
        examNumber = Integer.parseInt(in.readLine());
        System.out.print("The average for Exam #" + (examNumber) + " is ");
//        for (int cnt2 = 0; cnt2 < numberOfStudents; cnt2++) {
//            total = total + grades[cnt2][examNumber - 1];
//        }
        double average = total / numberOfStudents;
        System.out.println(average);

        if (average >= 90) {
            System.out.println("The average for Exam #" + examNumber + " is an A.");
        } else if (average >= 80 && average <= 89) {
            System.out.println("The average for Exam #" + examNumber + " is a B.");
        } else if (average >= 70 && average <= 79) {
            System.out.println("The average for Exam #" + examNumber + " is a C.");
        } else if (average >= 60 && average <= 69) {
            System.out.println("The average for Exam #" + examNumber + " is a D.");
        } else if (average <= 59) {
            System.out.println("The average for Exam # " + examNumber + " is an F.");
        }
    }

    public static void cleanUp(ArrayList<String> students, ArrayList<Integer> grades, int numberOfStudents,
                               int numberOfExams) throws IOException {
// set up output file
        PrintWriter outFile;
        outFile = new PrintWriter(new FileWriter("Gradebook.txt"));
        outFile.println(numberOfStudents);
        outFile.println(numberOfExams);
        // write array to output file
        for (int row = 0; row < numberOfStudents; row++) {
            outFile.println(students.get(row));
            for (int col = 0; col < numberOfExams; col++) {
                outFile.println(grades.get(col));
            }
        }
        outFile.close();
        System.out.println("The Grade Book file is saved so you can");
        System.out.println("pick up next time where you left off.\n");
    }
}

Comments

0

I have changed one method to show how to use ArrayList. Since grades are 2 dimensional you have to define it as ArrayList of ArrayList and access its contains as grades.get(cnt1).get(cnt2). To add a grade you can use the add method grades.get(cnt1).add(cnt2).

 public static void allStudentAverage(ArrayList<String> students, 
             ArrayList<ArrayList<Integer>> grades,
            int numberOfStudents,int numberOfExams)
    {
        double total = 0;
        System.out.println("Here are the averages for all students.\n");
        for (int cnt1 = 0; cnt1<numberOfStudents; cnt1++)
        {
            System.out.print("The average for " + students.get(cnt1) + " is ");
            for (int cnt2 = 0; cnt2<numberOfExams; cnt2++)
            {
                total = total + grades.get(cnt1).get(cnt2);
            }
            System.out.println(total/numberOfExams);
            total = 0;
        }
    }

Comments

0

I will give you the following example

String [] names = {"John","Bill","Sam"};
List arrylist =Arrays.asList(names);

That will convert your array to an array list, if that was the answer you were searching for.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.