Question: My code keeps getting the error: Invalid student data: 1 0 0 Exception in thread main java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1

My code keeps getting the error: Invalid student data: 100
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
at UniversitySystem.readUniversityData(UniversitySystem.java:51)
at UniversitySystem.main(UniversitySystem.java:84)
Here is my code:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class UniversitySystem {
private ArrayList students;
private ArrayList universities;
public UniversitySystem(){
students = new ArrayList>();
universities = new ArrayList>();
}
public void readStudentData(String filename){
try (BufferedReader reader = new BufferedReader(new FileReader(filename))){
String line;
while ((line = reader.readLine())!= null){
String[] data = line.split(",");
if (data.length >=13){// Ensure the array has enough elements
Student student = new Student(
data[0].trim(), data[1].trim(), data[2].trim(),// Trim whitespace from each field
Integer.parseInt(data[3].trim()), data[4].trim().charAt(0),
data[5].trim(), data[6].trim(), data[7].trim(), data[8].trim(), data[9].trim(),
data[10].trim().charAt(0), data[11].trim().charAt(0), data[12].trim().charAt(0)
);
students.add(student);
} else {
System.err.println("Invalid student data: "+ line);
}
}
} catch (IOException e){
System.err.println("Error reading student data from file: "+ e.getMessage());
} catch (NumberFormatException e){
System.err.println("Error parsing student age: "+ e.getMessage());
} catch (Exception e){
System.err.println("An unexpected error occurred: "+ e.getMessage());
}
}
public void readUniversityData(String filename) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(filename));
String line;
while ((line = reader.readLine())!= null){
String[] data = line.split(",");
University university = new University(
data[0], data[1], data[2], data[3], data[4],
Double.parseDouble(data[5]), Double.parseDouble(data[6]), Double.parseDouble(data[7]),
Double.parseDouble(data[8]), Double.parseDouble(data[9]), Double.parseDouble(data[10]),
Double.parseDouble(data[11]), Double.parseDouble(data[12]), Double.parseDouble(data[13]),
Double.parseDouble(data[14]), Double.parseDouble(data[15])
);
universities.add(university);
}
reader.close();
}
public void generateReports(){
for (University university : universities){
System.out.println("University Name: "+ university.getName());
System.out.println("Students:");
for (Student student : students){
if (student.getUniversity().equals(university.getName())){
System.out.println(student);
}
}
System.out.println("Meal Plan Information:");
System.out.println("Meal Plan A: $"+ university.getStudentCostMealPlanA());
System.out.println("Meal Plan B: $"+ university.getStudentCostMealPlanA());
System.out.println("Meal Plan C: $"+ university.getStudentCostMealPlanA());
}
}
public static void main(String[] args) throws IOException {
UniversitySystem system = new UniversitySystem();
system.readStudentData("C:\\Users\\Sarah\\Downloads\\StudentFile-1.txt");
system.readUniversityData("C:\\Users\\Sarah\\Downloads\\Universities9-1.txt");
system.generateReports();
}
}
This is the universities txt file:
10
Ohio State University, 1234 South
St,Columbus, OH,43210,1234567890,150.00,100.00,50.00,500.00,450.00,400.00,5
000.00,4000.00,3000.00,40.00,35.00,30.00,.25,10.00,150.00
University of Washington, 1600 Northeast
Ave, Seattle, WA,88940,1987654321,235.34,200.12,150.25,465.44,388.76,300.05,
4935.50,3564.74,2974.35,35.20,30.40,20.00,.15,20.99,300.00
University of Louisiana, 700 University
Avenue, Monroe, LA,71209,4987654321,235.34,200.12,150.25,465.44,388.76,300.0
5,4935.50,3564.74,2974.35,35.20,30.40,20.00,.15,20.99,300.00
Oklahoma State University, 700N Greenwood
Ave, Tulsa, OK,74106,2234567890,150.00,100.00,50.00,500.00,450.00,400.00,500
0.00,4000.00,3000.00,40.00,35.00,30.00,.25,10.00,150.00
George Mason University, 4400 University
Drive, Fairfax,Virg
My code keeps getting the error: Invalid student

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!