'm getting the error (below) when trying to run the program. My debugging skills aren't too great, so thanks in advance for the help.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

I'm getting the error (below) when trying to run the program. My debugging skills aren't too great, so thanks in advance for the help.

 

ERROR:

"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 2 at TestStudent.main(TestStudent.java:26)" 

CODE:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class TestStudent {

private static final String STUDENT_FILE = "ClassesData.dat";
private static final String COURSE_FILE = "CoursesData.dat";

public static void main(String[] args)
{
Scanner studentFileReader, courseFileReader;
Student student = null;
Course course = null;


try
{
studentFileReader = new Scanner(new File(STUDENT_FILE));
while(studentFileReader.hasNextLine())
{
String line = studentFileReader.nextLine().trim();
String[] data = line.split(",");
String name = data[0];
String major = data[1];
String className = data[2];
int credits = Integer.parseInt(data[3]);
char grade = data[4].charAt(0);

student = new Student(name, major, className, credits, grade);
}
studentFileReader.close();
}catch(FileNotFoundException fnfe){
System.out.println(STUDENT_FILE + " could not be found!");
System.exit(0);
}

try
{
courseFileReader = new Scanner(new File(COURSE_FILE));
while(courseFileReader.hasNextLine())
{
String line = courseFileReader.nextLine().trim();
String[] data = line.split(",");
int courseId = Integer.parseInt(data[0]);
int instructorId = Integer.parseInt(data[1]);
int roomId = Integer.parseInt(data[2]);

course = new Course(courseId, instructorId, roomId);
}
courseFileReader.close();
}catch(FileNotFoundException fnfe){
System.out.println(COURSE_FILE + " could not be found!");
System.exit(0);
}

displayDetails(student, course);
}

private static String giveComment(char grade)
{
if(grade == 'A')
return "Great Job!";
else
return "Best of luck next time!";
}

private static void displayDetails(Student st, Course co)
{
System.out.println("Student Name: " + st.getName() + "\n"
+ "Course Id: " + co.getCourseId() + "\n"
+ "Major: " + st.getMajor() + "\n"
+ "Class Name: " + st.getClassName() + "\n"
+ "Instructor Id: " + co.getInstructorId() + "\n"
+ "Room Id: " + co.getRoomId() + "\n"
+ "Grade: " + st.getGrade() + "\n"
+ "Credits: " + st.getCredits() + "\n"
+ "Comment: " + giveComment(st.getGrade()));
}
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Intelligent Machines
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education