This is what my program looks like
Error Codeimport java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; public class mypa9class { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // get all files and input in arrays // name array int max = 9; File last = new File("Lname.txt"); Scanner sclast = new Scanner(last); File first = new File("Fname.txt"); Scanner scfirst = new Scanner(first); String[] namearray = new String[max]; String firstname; String lastname; for (int i = 0; i < 10; i++) { firstname = scfirst.next(); lastname = sclast.next(); namearray[i] = firstname + " " + lastname; } // exam 1 double average1 = 0; double max1 = 0; double min1 = 0; File exam1 = new File("Exam1.txt"); Scanner scexam1 = new Scanner(exam1); double[] exarray1 = new double[max]; int count = 0; while (scexam1.hasNext()) { exarray1[count] = scexam1.nextDouble(); count++; } average1 = getAvg(exarray1); // System.out.println("average1 " + average1); max1 = getMax(exarray1); // System.out.println("max1 " + max1); min1 = getMin(exarray1); // System.out.println("min1 " + min1); // exam 2 double average2 = 0; double max2 = 0; double min2 = 0; File exam2 = new File("Exam2.txt"); Scanner scexam2 = new Scanner(exam2); double[] exarray2 = new double[max]; count = 0; while (scexam2.hasNext()) { exarray2[count] = scexam2.nextDouble(); count++; } average2 = getAvg(exarray2); // System.out.println("average2 " +average2); max2 = getMax(exarray2); // System.out.println("max2 " + max2); min2 = getMin(exarray2); // System.out.println("min2 " + min2); // exam 3 double average3 = 0; double max3 = 0; double min3 = 0; File exam3 = new File("Exam3.txt"); Scanner scexam3 = new Scanner(exam3); double[] exarray3 = new double[max]; count = 0; while (scexam3.hasNext()) { exarray3[count] = scexam3.nextDouble(); count++; } average3 = getAvg(exarray3); // System.out.println("average3 " + average3); max3 = getMax(exarray3); // System.out.println("max3 " +max3); min3 = getMin(exarray3); // System.out.println("min3 " + min3); // exam 4 double average4 = 0; double max4 = 0; double min4 = 0; File exam4 = new File("Exam4.txt"); Scanner scexam4 = new Scanner(exam4); double[] exarray4 = new double[max]; count = 0; while (scexam4.hasNext()) { exarray4[count] = scexam4.nextDouble(); count++; } average4 = getAvg(exarray4); // System.out.println("average4 " + average4); max4 = getMax(exarray4); // System.out.println("max4 " + max4); min4 = getMin(exarray4); // System.out.println("min4 " + min4); // System.out.println(namearray[9]); // System.out.println(exarray4[9]); // calculate average for students and input in a new array called exAvg double[] exAvg = new double[max]; for (int i = 0; i < 10; i++) { exAvg[i] = computeAverage(exarray1[i], exarray2[i], exarray3[i], exarray4[i]) ; } // calculate grade for each student and input it in a new array titled grade char[] grade = new char[max]; for (int i = 0; i < 10; i++) { grade[i] = computeGrade(exAvg[i]); } //output area + printwriter PrintWriter prw = new PrintWriter("PAOutFile.txt"); System.out.println("Full Name of Student \t\t Ex1 \t Ex2 \t Ex3 \t Ex4 \t Avg \t Grade"); prw.println("Full Name of Student \t\t Ex1 \t Ex2 \t Ex3 \t Ex4 \t Avg \t Grade"); for (int i = 0; i < 10; i++) { System.out.println(namearray[i] + "\t\t\t" + exarray1[i] + "\t" + exarray2[i] + "\t" + exarray3[i] + "\t" + exarray4[i] + "\t" + exAvg[i] + "\t" + grade[i]); prw.println(namearray[i] + "\t\t\t\t\t" + exarray1[i] + "\t" + exarray2[i] + "\t" + exarray3[i] + "\t" + exarray4[i] + "\t" + exAvg[i] + "\t" + grade[i]); } System.out.println(); System.out.println("Exam Number \t\t\t Max Score \t Min Score \t Average Score"); System.out.println("Exam 1 \t\t\t\t " + max1 + "\t\t" + min1 + "\t\t\t" + average1); System.out.println("Exam 2 \t\t\t\t " + max2 + "\t\t" + min2 + "\t\t\t" + average2); System.out.println("Exam 3 \t\t\t\t " + max3 + "\t\t" + min3 + "\t\t\t" + average3); System.out.println("Exam 4 \t\t\t\t " + max4 + "\t\t" + min4 + "\t\t\t" + average4); prw.println("Exam Number \t\t\t Max Score \t Min Score \t Average Score"); prw.println("Exam 1 \t\t\t\t " + max1 + "\t\t" + min1 + "\t\t\t" + average1); prw.println("Exam 2 \t\t\t\t " + max2 + "\t\t" + min2 + "\t\t\t" + average2); prw.println("Exam 3 \t\t\t\t " + max3 + "\t\t" + min3 + "\t\t\t" + average3); prw.println("Exam 4 \t\t\t\t " + max4 + "\t\t" + min4 + "\t\t\t" + average4); prw.close(); } public static double getAvg(double[] exam) { double average = 0; double total = 0; //average exam for (int i = 0; i < 10; i++) { total = total + exam[i]; } average = total/10; return(average); } public static double getMax(double[] exam) { double max = 0; for (int i = 0; i < 10; i++) { if (max < exam[i]) { max = exam[i]; } } return(max); } public static double getMin(double[] exam) { double min = 999; for (int i = 0; i <10; i ++) { if (min > exam[i]) { min = exam[i]; } } return(min); } public static double computeAverage(double exam1, double exam2, double exam3, double exam4) { double studavg = 0; studavg = (exam1 + exam2 + exam3 + exam4) / 4; return(studavg); } public static char computeGrade(double avgrade) { char grade = 'E'; if (avgrade >= 90) { grade = 'A'; } if (avgrade < 90 && avgrade >= 80) { grade = 'B'; } if (avgrade < 80 && avgrade >= 70) { grade = 'C'; } if (avgrade < 70 && avgrade >= 60) { grade = 'D'; } if (avgrade <60) { grade = 'E'; } return (grade); } }
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9 at mypa9class.main(mypa9class.java:32)
There is much more code, but i am just wondering why this gives me an error, when i switch the length of the array to 10 instead of 9 i have no problems. The scanners bring in names from two different text files both with have 10 names each. When making the array i figured 0,1,2, -> 9 would give me the 10 spots i need i don't understand why i need it a 10 which would in turn leave namearry[10] to have nothing in it?
Let me know if i didn't explain my question good enough.