I am getting an error that says Index 4 out of bounds for length 4 and i'm not sure what that means. I someone could please tell me what and where the error is it would be greatly appreciated.
The error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 4 out of bounds for length 4
My code:
package HW8;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class GradeBookDemo {
public static void main(String[] args) throws IOException {
GradeBook gradebook = new GradeBook();
File file = new File("StudentInfo.txt");
Scanner inputfile = new Scanner(file);
// read the student information
while (inputfile.hasNextLine()) {
for (int index = 0; index < 5; index++) {
String name = inputfile.nextLine();
gradebook.setName(index, name);
double[] scores = new double[4];
for (int i = 0; i < 4; i++) {
scores[index] = inputfile.nextDouble();
}
gradebook.setScore(index, scores);
inputfile.nextLine();
}
for (int index = 0; index < 5; index++) {
System.out.println("Student name: " + gradebook.getName(index));
System.out.println("Student average: " + gradebook.getAverage(index));
System.out.println("Student grade: " + gradebook.getLetterGrade(index));
System.out.println("");
}
}
}
}
My other code:
package HW8;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;
public class GradeBook {
private int NUM_STUDENT = 5;
private int NUM_TESTS = 4;
private String[] names = new String[5]; // Array to hold student names
private char[] grades = new char[5]; // Array to hold student grades
// Create arrays of scores, one for each student
private double[] scores1 = new double[NUM_TESTS];
private double[] scores2 = new double[NUM_TESTS];
private double[] scores3 = new double[NUM_TESTS];
private double[] scores4 = new double[NUM_TESTS];
private double[] scores5 = new double[NUM_TESTS];
// Create setName and setScore methods
public void setName(int studentNumber, String name) {
names[NUM_STUDENT - 1] = name;
}
public void setScore(int studentNumber, double[] scores) {
if (studentNumber == 1) {
copyArray(scores1, scores);
} else if (studentNumber == 2) {
copyArray(scores2, scores);
} else if (studentNumber == 3) {
copyArray(scores3, scores);
} else if (studentNumber == 4) {
copyArray(scores4, scores);
}
if (studentNumber == 5) {
copyArray(scores5, scores);
}
}
public String getName(int studentNum) // create getName method
{
return names[studentNum - 1];
}
public double getAverage(int studentNum) { // create getAverage method
if (studentNum == 1) {
return calcAverage(scores1);
} else if (studentNum == 2) {
return calcAverage(scores2);
} else if (studentNum == 3) {
return calcAverage(scores3);
} else if (studentNum == 4) {
return calcAverage(scores4);
}
if (studentNum == 5) {
return calcAverage(scores5);
}
return studentNum;
}
public char getLetterGrade(int studentNum) {
assignGrade(studentNum);
return grades[studentNum - 1];
}
private void copyArray(double[] to, double[] from) {
for (int i = 0; i < NUM_TESTS; i++) {
to[i] = from[i];
}
}
private double calcAverage(double[] scores) {
double total = 0;
for (int i = 0; i < scores.length; i++) {
total += scores[i];
}
double average = total / NUM_TESTS;
return average;
}
private void assignGrade(int studentNum) {
double average = getAverage(studentNum);
grades[studentNum - 1] = determineGrade(average);
}
private char determineGrade(double average) {
if (average >= 90.0) {
return 'A';
} else if (average >= 80.0) {
return 'B';
} else if (average >= 70.0) {
return 'C';
} else {
return 'D';
}
}
}