Exception in thread "main"
java.lang.ArrayIndexOutOfBoundsException: 6
at
assignment2.Client.readStudents(Client.java:64)
at assignment2.Client.main(Client.java:253)
void readStudents() {
// Scanner class object declare
Scanner readStudentFile = null;
// try block begin
/*System.out.println("Working Directory = " +
System.getProperty("user.dir")); // for debugging purposes
only*/
try {
// open file
readStudentFile = new Scanner(new File("student.txt"));
// loop until end of file
while (readStudentFile.hasNextLine()) {
String stu = readStudentFile.nextLine();
String[] eachStu;
eachStu = stu.split(" ");
String firstName = null, middleName = null, lastName = null;
long id = 0;
int mark1 = 0, mark2 = 0, mark3 = 0;
/*
* check for name is in correct format or not
*/
try {
firstName = eachStu[0];
middleName = eachStu[1];
lastName = eachStu[2];
} // catch block for invalid formatted line
catch (NullPointerException e) {
System.out.println("Name is not correct!");
}
// check for id
try {
id = Long.parseLong(eachStu[3]);
} // catch block for invalid formatted line
catch (NumberFormatException e) {
System.out.println("ID is not correct!");
}
// check for marks
try {
mark1 = Integer.parseInt(eachStu[4]);
mark2 = Integer.parseInt(eachStu[5]);
mark3 = Integer.parseInt(eachStu[6]); //THIS IS THE LINE 64 ERROR
LINE
}
catch (NumberFormatException e) {
System.out.println("Subjects mark is not correct");
}
students.add(new Student(firstName, middleName, lastName, id,
mark1, mark2, mark3));
} // end while
readStudentFile.close();// close file
} // end try
// catch block for file not found exception
catch (FileNotFoundException fe) {
System.out.println("\nERROR: unable to open Student
file.\n");
} // end of catch
}// end method
public static void main(String args[]) {
Client sr = new Client();
sr.readStudents(); //THIS IS THE LINE 253 ERROR
do {
switch(sr.menu()) {
case 1:
System.exit(0);
case 2:
sr.add();
break;
case 3:
sr.removeStudent();
break;
case 4:
sr.showCourseWorkStudent();
sr.showResearchStudents();
break;
case 5:
sr.showCourseWorkStudent();
break;
case 6:
sr. showResearchStudents();
break;
case 7:
sr.showCourseWorkStudentBelowAboveAvg();
break;
case 8:
sr.showResearchStudentBelowAboveAvg();
break;
case 9 :
System.out.println("\nEnter student ID number to search: ");
long id = sr.sc.nextLong();
int pos = sr.searchStudentID(id);
if(pos == -1) {
System.out.println("ERROR: No student found with ID number: " +
id);
}
else {
System.out.println(sr.students.get(pos));
}
break;
case 10:
System.out.println("Enter student surname to search: ");
String name = sr.sc.nextLine();
break;
default:
System.out.println("\nInvalid Choice.");
}
}
while(true);
}
}
Why am I getting these errors?
Input file: .txt file with (filename: student.txt)
Mr. Bruce Wayne 11111111 30 02 1939
Brain Mccallum 74782192 11 17 1965
Jeanette Urrutia 81998412 11 29 1961
Barbera Lawton 44959581 05 26 1997
Pauletta Bauman 47590802 09 08 1962
Marilynn Vanbeek 53759014 12 18 1971
Jacquetta Trott 22785872 12 10 1965
Bettye Yetman 14724361 12 10 1965
Andre Hoefler 54859353 08 02 1977
Haywood Thomasson 90148347 06 20 1989
Your code assumes that, each line of the text file is of the form:
FIRSTNAME MIDDLENAME LASTNAME ID MARK1 MARK2 MARK3
each field separated by a single blankspace. So, when you split the line using split(" "), you will get a String array of size 6 i.e. indices as 0,1,2...,5,6.
Now look at the text file you are trying to read. Except first line, the format of all other lines are as follows:
FIRSTNAME LASTNAME ID MARK1 MARK2 MARK3
So, there are only five fields per line separated by space. When you split using split(" "), you will get a String array of size 5 only, i.e. indices 0,1,2,3,4,5.
But you are still doing this at line 64:
mark3 = Integer.parseInt(eachStu[6]);
It means you are trying to read at index 6 from an array of size 5 (as explained above). Hence you are getting ArrayIndexOutOfBounds Exception.
To get rid of this, make your text file follow uniform formatting, like,
FIRSTNAME LASTNAME ID MARK1 MARK2 MARK3
for all the lines in the text file.
Get Answers For Free
Most questions answered within 1 hours.