Hello there,
I am making a two-dimensional array where you can input a maximum of 20 students and 7 grades, and am printing them out in a formatted table. The array calls for averaging each test's grades each student, giving an average of what the whole class scored on the test, needed to be printed bellow each column correctly. Then I must also be able to add information to the existing array, like adding more students. So far, I have gotten the enter of the information into the table format correct, but I can't find a way to do the last two requirements. I think I was on the verge of it, but it just errors.
Here's what I have:
import java.util.*; public class FormattedGradeArray { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner indata = new Scanner(System.in); // Establishes the rows and columns in the arrays, // along with the average grades. int students = 0, tests = 0; double sum = 0; // Establishes an array for student names, test grades and averages, and // the final letter grade. double[][] grade = new double[20][8]; String[] student = new String[20]; String[] letter = new String[20]; // This allows to set the number of students and test grades that you can enter. System.out.println("Please enter the number of students you want to enter(1-20): "); students = indata.nextInt(); System.out.println("Please enter the number of test grades (3-7): "); tests = indata.nextInt(); System.out.println(); // This allows you to enter the student name, and their test grades. for( int x = 0; x < students; x++){ System.out.println("Student Name: "); student[x] = indata.next(); for(int y = 0; y < tests; y++){ System.out.println("Test Grade " +(y + 1) +": "); grade[x][y] = indata.nextDouble(); sum += grade[x][y]; } // Determines grade letter from the average score of each student. grade[x][tests] = sum/tests; if((grade[x][tests] <= 100) && (grade[x][tests] >= 89.5)){ letter[x] = "A";} else if((grade[x][tests] <=89.5) && (grade[x][tests] >= 79.5)){ letter[x] = "B";} else if((grade[x][tests] <= 79.5) && (grade[x][tests] >= 69.5)){ letter[x] = "C";} else if((grade[x][tests] <= 69.5) && (grade[x][tests] >= 59.5)){ letter[x] = "D";} else if((grade[x][tests] <= 59.5) && (grade[x][tests] >= 0)){ letter[x] = "F";} sum = 0; System.out.println(); } // Output System.out.printf("%-10s","Student Name" + "\t"); for(int y = 0; y < tests; y++){ System.out.printf("%-10s", "Test " + (y + 1)); } System.out.println(" Average Letter"); for(int q = 0; q < students; q++){ System.out.printf("%-12s", student[q]); for(int w = 0; w < tests; w++){ System.out.printf("%10.2f", grade[q][w]); } System.out.printf("%12.2f", grade[q][tests]); System.out.printf("%13s", letter[q]); System.out.println(); } } }
Thank you in advance for your suggestions.