I have a text file with student's names on one line, followed by 15 grades (each separated by 2 spaces & with a single space at the beginning of the line) on the next line.
Example:
John, Elton
89 97 91 94 82 96 98 67 69 97 87 90 100 80 92
Johnson, Kay
96 68 71 70 90 79 69 63 88 75 61 77 100 80 88
(More data...)
I need to read the line containing the student name and store the value in a String array, then read the following line of grades, storing the values in 2D Integer array. On each line of integers, the first 10 values are to be considered assignment grades while the last 5 values are exam grades (To be used later when computing student averages). My program crashes due to an "Input Mismatch Exception" when it attempts to read the line of integers after the first student name. Here's my code so far:
import java.io.*;
import java.util.*;
public class Grades
{
// Declare constants for array lengths
private static final int ROWS = 25;
private static final int COLS = 15;
public static void main(String[] args) throws IOException
{
// Create new two dimensional integer array to hold students assignment & exam grades
int[][] grades = new int[ROWS][COLS];
// Create new string array to hold student names
String[] students = new String[ROWS];
// Total number of students
int numStudents;
// Display your name
System.out.println( "YOUR NAME" );
// Fill the arrays
numStudents = fillArray( grades, students );
// Display the data that was just read in
display(grades, students, numStudents);
}
// The fillArray method opens & reads student name & grade data from a text file
private static int fillArray(int[][] sGrades, String[] sNames) throws IOException
{
// Students counter
int students = 0;
// Create the file
File studentGrades = new File("Grades.txt");
// Check that the file exists
if (!studentGrades.exists())
{
// Display error message and exit the program
System.out.println(studentGrades + " not found. Aborting.");
System.exit(0);
}
// Create scanner object to read from the file
Scanner gradeFile = new Scanner(studentGrades);
// Read data until the end of the file is reached or the array is full
while (gradeFile.hasNext() && students < sNames.length)
{
// Begin filling the array of student names starting with the first element
for ( int i = 0; i < ROWS; i++ )
{
// Store the students name into the array's current index
sNames[i] = gradeFile.nextLine();
// Increment the number of students
students++;
// Begin filling in the array of grades starting with the first element
/************* ERROR OCCURS HERE ********************/
for ( int j = 0; j < COLS; j++ )
// Store the grade into the array's current index
sGrades[i][j] = gradeFile.nextInt();
/************** *******************/
}
}
// Close the file
gradeFile.close();
// Return the total number of students
return students;
}
// The display method prints Each student's name, 10 assignment
// grades and 5 exam grades
// to the screen, separating each student by a blank line.
total number of students
private static void display(int[][] gradeArray, String [] studentArray, int totalStudents)
{
//
for ( int i = 0; i < totalStudents; i++)
{
System.out.println();
//
System.out.print("STUDENT: " + studentArray[i]);
System.out.print("\nASSIGNMENTS: ");
//
for ( int j = 0; j < 10; j++)
//
System.out.print(gradeArray[i][j] + " ");
//
System.out.print("\nEXAMS: ");
//
for ( int k = 10; k < COLS; k++)
//
System.out.print(gradeArray[i][k] + " ");
System.out.println();
}
The output from the 'display' method should look like this:
YOUR NAME
STUDENT: John, Elton
ASSIGNMENTS: 89 97 91 94 82 96 98 67 69 97
EXAMS: 87 90 100 80 92
STUDENT: Johnson, Kay
ASSIGNMENTS: 96 68 71 70 90 79 69 63 88 75
EXAMS: 61 77 100 80 88
(More output...)
Since the program crashes before I can see any output, I removed the following line of code that is trying to read the next Integer to see what is happening.
for ( int j = 0; j < COLS; j++ )
sGrades[i][j] = gradeFile.nextInt();
As I expected, each line of the file is read as a string and the value is stored in the array of student names resulting in output like this:
STUDENT: John, Elton
ASSIGNMENTS: 0 0 0 0 0 0 0 0 0 0
EXAMS: 0 0 0 0 0
STUDENT: 89 97 91 94 82 96 98 67 69 97 87 90 100 80 92
ASSIGNMENTS: 0 0 0 0 0 0 0 0 0 0
EXAMS: 0 0 0 0 0
STUDENT: Johnson, Kay
ASSIGNMENTS: 0 0 0 0 0 0 0 0 0 0
EXAMS: 0 0 0 0 0
STUDENT: 96 68 71 70 90 79 69 63 88 75 61 77 100 80 88
ASSIGNMENTS: 0 0 0 0 0 0 0 0 0 0
EXAMS: 0 0 0 0 0
(More output...)
My brain hurts trying to figure out where I'm going wrong. If I were to guess, I would think it has to do with the spaces separating each integer causing each line of grades to be interpreted as a String instead of an integer. Or is there something else I may have overlooked?
EDIT: I am limited to using more basic Java techniques for this program. I cannot use Array Lists, exception handling other than a throws
clause in the method headers. File-handling is limited to using the File
& Scanner
classes. No BufferReader
or StringReader
or FileReader
.