0

I am getting this error when trying to run my program.

Can someone help me figure it out?

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
at university_info.UniversityDriver.main(UniversityDriver.java:39)

My code:

UniversityDriver.java

package university_info;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Scanner;

public class UniversityDriver{

   public static void main(String[] args) {
       // TODO Auto-generated method stub
       University u_city=new University("hero university","Teach well");
       System.out.println("Welcome to "+u_city.universityName+" Univercity");  
   System.out.println(u_city.motto);
     
  
   // retrieving data from external file on startup.....
   ArrayList<Person> al=new ArrayList<Person>();
   try{
       FileInputStream fis = new FileInputStream("fileName.txt");
       ObjectInputStream ois = new ObjectInputStream(fis);
       al = (ArrayList<Person>)ois.readObject();
       ois.close();
       }catch(FileNotFoundException ex){
           System.out.println("File not found exception");
       }catch(IOException ex){
           ex.printStackTrace();
       }catch(ClassNotFoundException ex){
          
       }
  
   Object[] array=new Object[al.size()];
   array=al.toArray(array);
  
   for(int i=0;i<=array.length;i++){
       u_city.people[i]=(Person)array[i];
   }
      
   System.out.println("What would you like to do");
   System.out.println("Enter 'hire' to hire a new faculty member. ");
   System.out.println("Enter 'admit' to admit a new student ");
   System.out.println("Enter 'find student' to list information about a student");
   System.out.println("Enter 'find faculty' to list information about a faculty member");
   System.out.println("Enter 'list students' to list the names of all students.");
   System.out.println("Enter 'list faculty' to list the names of faculty members");
   System.out.println("Enter 'quit' to end this program and save data");
  
   //-------------------->
  
  
   Scanner in =new Scanner(System.in);
   boolean run= true;
   while(run){
      
   String s= in.nextLine();
   if(s.equals("quit")){
       run=false;
       //save data in extenal file
   try{
      FileOutputStream fos = new FileOutputStream("fileName.txt");
          ObjectOutputStream oos = new ObjectOutputStream(fos);
          oos.writeObject(u_city.people);
          oos.close();
      }catch(IOException ex){
          ex.printStackTrace();
      }
   }
     
   if(s.equals("hire")){
       u_city.hire();
   }
   if(s.equals("admit")){
       u_city.admit();
   }
   if(s.equals("find_student")){
       System.out.println("What is the student's first name?");
       String fn=in.nextLine();
       System.out.println("What is the student's last name?");
       String ln=in.nextLine();
       Student temp=u_city.findStudent(fn, ln);
       if(temp==null){
           System.out.println("Student not found");
       }else{
           temp.Details();
       }
   }
   if(s.equals("find_faculty")){
       System.out.println("What is the faculty's first name?");
       String fn=in.nextLine();
       System.out.println("What is the faculty's last name?");
       String ln=in.nextLine();
       Faculty temp=u_city.findFaculty(fn, ln);
       if(temp==null){
           System.out.println("faculty not found");
       }
   }
  
   if(s.equals("list_student")){
       Person[] temp= u_city.getStudents();
       for(int i=0;i<=temp.length;i++){
           System.out.println(temp[i].firstName+" "+temp[i].lastName);
       }
         
   }
   if(s.equals("list_faculty")){
       Person[] temp= u_city.getFaculty();
       for(int i=0;i<=temp.length;i++){
           System.out.println(temp[i].firstName+" "+temp[i].lastName);
       }     
         
   }
   }
  
}
}  

Faculty.java

package university_info;

public class Faculty extends Person {
   String[] courses; // courses that faculty member teaches
  
   public Faculty(String first,String last,int m,int d,int y,String[] course,boolean bol){
  
   this.courses=course; //----------->
  
   this.firstName=first;
   this.lastName=last;
   this.monthBirth=m;
   this.dayBirth=d;
   this.yearBirth=y;
   this.student_or_faculty=bol;
   }
  
   public void Details_Faculty(){
       System.out.println("Student:"+ this.firstName +" "+ this.lastName);
       System.out.println("DOB: "+ this.dayBirth+"/"+this.monthBirth+"/"+this.yearBirth);
       System.out.println("Courses: ");
       for(int i=0;i<=courses.length;i++){
           System.out.println(courses[i]);
       }
   }
}

Person.java

package university_info;

public class Person {
   String firstName;
   String lastName;
   int monthBirth;
   int dayBirth;
   int yearBirth;
   boolean student_or_faculty; // true for student and false for faculty
  
   public Person(){} //must have a constructor
}

School.java

package university_info;

public interface School {
   Student findStudent(String fn,String ln);
   Faculty findFaculty(String fn,String ln);
  
   Faculty hire();
   Student admit();
  
   Person[] getAllPersons();
   String[] getAllMajors();
  
   String[] getAllCourses();
  
   Person[] getStudents();
   Person[] getFaculty();
}

Student.java

package university_info;

public class Student extends Person {
   String major;
  
   public Student(String first,String last,int m,int d,int y,String major,boolean bol){
      
       this.major=major;
      
       this.firstName=first;
       this.lastName=last;
       this.monthBirth=m;
       this.dayBirth=d;
       this.yearBirth=y;
       this.student_or_faculty=bol;
   } // must have a constructor
  
   public void Details(){
       System.out.println("Student:"+ this.firstName +" "+ this.lastName);
       System.out.println("DOB: "+ this.dayBirth+"/"+this.monthBirth+"/"+this.yearBirth);
       System.out.println("Major: "+this.major);
   }
}

University.java

package university_info;

import java.util.Scanner;

public class University implements School {
  
   int MAX_SIZE= 500;
  
   String universityName;
   String motto;
   Person[] people;
   String[] majors;
   String[] courses;
  
   public University(String u_Name,String u_motto){
       this.universityName=u_Name;
       this.motto=u_motto;
      
       people=new Person[MAX_SIZE];
      
       majors=new String[4];
       majors[0]= "Hardware Architecture";
       majors[1]= "Information Analytics";
       majors[2]= "Quantum Computing";
       majors[3]= "Undecided";
      
       courses=new String[12];
       courses[0]="Computers";
       courses[1]="Advance Physics";
       courses[2]="Quantum Entanglement";
       courses[3]="Parallel Programming";
       courses[4]="Advance Algorithums";
       courses[5]="FPGA Programming";
       courses[6]="Hardware Design";
       courses[7]="Embedded Systems";
       courses[8]="Signal Processing";
       courses[9]="Artificial Intelligence";
       courses[10]="Bayesian Logic";
       courses[11]="Probablity";
   }

   public Student findStudent(String fn, String ln) {
       // TODO Auto-generated method stub
       for(int i=0;i<=people.length;i++){
       if(people[i].firstName.equals(fn)&& people[i].lastName.equals(ln));
       return (Student)people[i];
       }
      
       return null;
   }

   public Faculty findFaculty(String fn, String ln) {
       // TODO Auto-generated method stub
       for(int i=0;i<=people.length;i++){
           if(people[i].firstName.equals(fn)&& people[i].lastName.equals(ln));
           return (Faculty)people[i];
           }
      
      
       return null;
   }

   public Faculty hire() {
       // TODO Auto-generated method stub
      
       Scanner in =new Scanner(System.in);
       String s= in.nextLine();
       System.out.println("You entered string "+s);
      
       System.out.println("What is the person's first name?");
       String f_name= in.nextLine();
       System.out.println("What is the person's last name?");
       String l_name= in.nextLine();
       System.out.println("What is the person's month of birth?");
       int m= in.nextInt();
       System.out.println("What is the person's day of birth?");
       int d= in.nextInt();
       System.out.println("What is the person's year of birth?");
       int y= in.nextInt();
      
       String[] c=new String[20];// max 20 courses can be assigned to faculty
       int i=0;
       boolean condition=true;

       while(condition) {
           System.out.println("Assign a course to this Faculty enter 'done' if there are no other courses?");
           String course= in.nextLine();

           if(!course.equals("done")) {
               c[i]=course;
               i++;
           }
           condition=false;
       }
      
       boolean b=false;
       Faculty temp=new Faculty(f_name,l_name,m,d,y,c,b);      
       return temp;
   }

   public Student admit() {
       // TODO Auto-generated method stub
       Scanner in =new Scanner(System.in);
       String s= in.nextLine();
       System.out.println("You entered string "+s);
      
       System.out.println("What is the person's first name?");
       String f_name= in.nextLine();
       System.out.println("What is the person's last name?");
       String l_name= in.nextLine();
       System.out.println("What is the person's month of birth?");
       int m= in.nextInt();
       System.out.println("What is the person's day of birth?");
       int d= in.nextInt();
       System.out.println("What is the person's year of birth?");
       int y= in.nextInt();
       System.out.println("What is the person's major?");
       String major= in.nextLine();
      
       boolean b=true;
       Student temp=new Student(f_name,l_name,m,d,y,major,b);
       return temp;
   }

   public Person[] getAllPersons() {
       // TODO Auto-generated method stub
       return this.people;
   }

   public String[] getAllMajors() {
       // TODO Auto-generated method stub
       return this.majors;
   }

   public String[] getAllCourses() {
       // TODO Auto-generated method stub
       return this.courses;
   }

   public Person[] getStudents() {
       // TODO Auto-generated method stub
       Person[] temp=new Person[MAX_SIZE];
       for(int i=0;i<=MAX_SIZE;i++){
           if(people[i].student_or_faculty == true){
               temp[i]=people[i];
           }
       }
      
       return temp;
   }

   public Person[] getFaculty() {
       // TODO Auto-generated method stub
       Person[] temp=new Person[MAX_SIZE];
       for(int i=0;i<=MAX_SIZE;i++){
           if(people[i].student_or_faculty == false){
               temp[i]=people[i];
           }
       }
      
       return temp;
   }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

1

Changing this part:

for (int i = 0; i <= array.length; i++) {
        u_city.people[i] = (Person) array[i];
    }

to

for (int i = 0; i < array.length; i++) {
        u_city.people[i] = (Person) array[i];
    }

will resolve the issue. The loop cannot exceed the size of array, and since the index i starts from 0, maximum index number could be array's size minus one.

Hope this helps out.

Ali K. Nouri
  • 495
  • 5
  • 18
  • now I get this error. java.io.StreamCorruptedException: invalid stream header: 22427275 at java.base/java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:964) at java.base/java.io.ObjectInputStream.(ObjectInputStream.java:403) at university_info.UniversityDriver.main(UniversityDriver.java:24) – Un Coup De Feu Oct 09 '20 at 04:56
  • this is the 24th line : ObjectInputStream ois = new ObjectInputStream(fis); – Un Coup De Feu Oct 09 '20 at 04:57
  • Why are you using `ObjectInputStream` to read a text file? It can only be used to **binary** files written by `ObjectOutputStream`. It is explained in the javadocs. – Stephen C Nov 06 '20 at 09:55