I have an assignment to read an input file made by me using one line for the students name and the next ten lines for quiz scores. I am supposed to use a bubble sort for the scores and then outprint the name and the top 8 quiz scores for each person. I can get it to work with just the first person, but then run in to one of two exception errors.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at QuizScores.main(QuizScores.java:77)
or
Exception in thread "main" java.lang.NumberFormatException: For input string: "Timothy Cretsinger"
at java.lang.NumberFormatException.forInputString(Num berFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:449)
at java.lang.Integer.parseInt(Integer.java:499)
at QuizScores.main(QuizScores.java:44)
here is what the input looks like:
Patrick Tilley
67
84
89
78
91
94
88
87
88
93
Timothy Cretsinger
84
86
85
91
87
83
81
91
92
80
and it goes on for 5 people. I understand the exception errors, but can't seem to figure out how to fix it. I know that when I go to read the next 11 lines for the array it can't convert the name to integer, and if I decrease the array to avoid the name, I get the out of bounds error.
here is what my code looks like:
import java.io.*; public class QuizScores { public static void main(String args[])throws Exception { // Declare variables. final String TITLE = "\n\nRuss Penning Quiz Report\n\n"; final int SIZE = 11; // Number of quizzes int score[] = new int[SIZE]; // Array used to store 10 quiz scores. String stuName; int x; final int LIMIT = SIZE; String stuScoreString; int stuScore = 0; int pairsToCompare; boolean switchOccurred; boolean done; int temp; // Input student's name FileReader fr = new FileReader("scores.dat"); BufferedReader br = new BufferedReader(fr); // This is the work done in the fillArray() method if((stuName = br.readLine()) != null) { x = 0; done = false; while(x < LIMIT) { // Place value in array. stuScoreString = br.readLine(); // Convert String to integer. stuScore = Integer.parseInt(stuScoreString); score[x] = stuScore; x++; // Get ready for next input item. } // End of input loop. stuScore = x - 1; pairsToCompare = stuScore - 1; // This is the work done in the sortArray() method switchOccurred = true; // set flag to true while(switchOccurred == true) { x = 0; switchOccurred = false; while(x < pairsToCompare) { if(score[x] > score[x + 1]) { temp = score[x + 1]; score[x+1] = score[x]; score[x] = temp; switchOccurred = true; } x++; } pairsToCompare--; } // This is the work done in the displayArray() method x = 0; System.out.println(stuName); while(x < stuScore) { System.out.println(score[x + 2]); x++; } } else done = true; br.close(); System.exit(0); } // End of main() method. } // End of QuizScores class.
If anyone has any idea what I am doing wrong, please let me know, and thank you for all your help.