Index 0 out of bounds for length 0 at university_info.UniversityDriver.main(UniversityDriver.java:39)

huangapple go评论103阅读模式
英文:

Index 0 out of bounds for length 0 at university_info.UniversityDriver.main(UniversityDriver.java:39)

问题

以下是您提供的代码的翻译部分:

我在尝试运行我的程序时遇到了这个错误

有人可以帮我找出问题吗

> 在线程main中的异常java.lang.ArrayIndexOutOfBoundsException索引0超出了长度0的界限  
> 位于university_info.UniversityDriver.main(UniversityDriver.java:39)

我的代码

UniversityDriver.java

    包 university_info;
    导入 java.io.FileInputStream;
    导入 java.io.FileNotFoundException;
    导入 java.io.FileOutputStream;
    导入 java.io.IOException;
    导入 java.io.ObjectInputStream;
    导入 java.io.ObjectOutputStream;
    导入 java.util.ArrayList;
    导入 java.util.Scanner;
    
    公共类 UniversityDriver {
    
       公共静态无效 main(String[] args) {
           // TODO 自动生成的方法存根
           大学 u_city = new University("hero university","好好教学");
           System.out.println("欢迎来到" + u_city.universityName + "大学");
           System.out.println(u_city.motto);
         
      
           // 在启动时从外部文件检索数据...
           ArrayList<Person> al = new ArrayList<Person>();
           尝试 {
               FileInputStream fis = new FileInputStream("fileName.txt");
               ObjectInputStream ois = new ObjectInputStream(fis);
               al = (ArrayList<Person>) ois.readObject();
               ois.close();
           } 捕获 (FileNotFoundException ex) {
               System.out.println("文件未找到异常");
           } 捕获 (IOException ex) {
               ex.printStackTrace();
           } 捕获 (ClassNotFoundException ex) {
              
           }
      
           对象[] 数组 = 新对象[al.size()];
           array = al.toArray(array);
      
           对于 (int i = 0; i <= array.length; i++) {
               u_city.people[i] = (Person) array[i];
           }
          
           System.out.println("您想要做什么?");
           System.out.println("输入'雇佣'以雇佣新的教职员工。 ");
           System.out.println("输入'录取'以录取新的学生 ");
           System.out.println("输入'查找学生'以列出有关学生的信息");
           System.out.println("输入'查找教职员工'以列出有关教职员工的信息");
           System.out.println("输入'列出学生'以列出所有学生的姓名。");
           System.out.println("输入'列出教职员工'以列出教职员工的姓名");
           System.out.println("输入'退出'以结束此程序并保存数据");
      
           //-------------------->
      
           扫描器 在 = 新扫描器(System.in);
           布尔值 运行 =;
(运行) {
               字符串 s =.nextLine();
               如果 (s.equals("退出")) {
                   运行 =;
                   // 将数据保存在外部文件中
                   尝试 {
                       FileOutputStream fos = new FileOutputStream("fileName.txt");
                       ObjectOutputStream oos = new ObjectOutputStream(fos);
                       oos.writeObject(u_city.people);
                       oos.close();
                   } 捕获 (IOException ex) {
                       ex.printStackTrace();
                   }
               }
         
               如果 (s.equals("雇佣")) {
                   u_city.hire();
               }
               如果 (s.equals("录取")) {
                   u_city.admit();
               }
               如果 (s.equals("查找学生")) {
                   System.out.println("学生的名字是什么?");
                   字符串 fn =.nextLine();
                   System.out.println("学生的姓氏是什么?");
                   字符串 ln =.nextLine();
                   学生 temp = u_city.findStudent(fn, ln);
                   如果 (temp == null) {
                       System.out.println("未找到学生");
                   } 否则 {
                       temp.Details();
                   }
               }
               如果 (s.equals("查找教职员工")) {
                   System.out.println("教职员工的名字是什么?");
                   字符串 fn =.nextLine();
                   System.out.println("教职员工的姓氏是什么?");
                   字符串 ln =.nextLine();
                   教职员工 temp = u_city.findFaculty(fn, ln);
                   如果 (temp == null) {
                       System.out.println("未找到教职员工");
                   }
               }
      
               如果 (s.equals("列出学生")) {
[] temp = u_city.getStudents();
                   对于 (int i = 0; i <= temp.length; i++) {
                       System.out.println(temp[i].firstName + " " + temp[i].lastName);
                   }
             
               }
               如果 (s.equals("列出教职员工")) {
[] temp = u_city.getFaculty();
                   对于 (int i = 0; i <= temp.length; i++) {
                       System.out.println(temp[i].firstName + " " + temp[i].lastName);
                   }     
               }
           }
       }
    }  

Faculty.java

    包 university_info;
    
    公共类 Faculty 扩展 Person {
       字符串[] 课程; // 教职员工教授的课程
      
       公共教职员工(String first, String last, int m, int d, int y, String[] course, 布尔值 bol) {
      
           这个.课程 = 课程; //----------->
      
           这个.firstName = first;
           这个.lastName = last;
           这个.monthBirth = m;
           这个.dayBirth = d;
           这个.yearBirth = y;
           这个.student_or_faculty = bol;
       }
      
       公共无效 详情_教职员工() {
           System.out.println("学生:" + 这个.firstName + " " + 这个.lastName);
           System.out.println("出生日期: " + 这个.dayBirth + "/" + 这个.monthBirth + "/" + 这个.yearBirth);
           System.out.println("课程: ");
           对于 (int i = 0; i <= 课程.length; i++) {
               System.out.println(课程[i]);
           }
       }
    }

Person.java

    包 university_info;
    
    公共类 Person {
       字符串 firstName;
       字符串 lastName;
       整数 monthBirth;
       整数 dayBirth;
       整数 yearBirth;
       布尔值 student_or_faculty; // 学生为真,教职员工为假
      
       公共人() {} //

<details>
<summary>英文:</summary>

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

Can someone help me figure it out?

&gt; Exception in thread &quot;main&quot; java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0  
&gt; 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(&quot;hero university&quot;,&quot;Teach well&quot;);
           System.out.println(&quot;Welcome to &quot;+u_city.universityName+&quot; Univercity&quot;);  
       System.out.println(u_city.motto);
         
      
       // retrieving data from external file on startup.....
       ArrayList&lt;Person&gt; al=new ArrayList&lt;Person&gt;();
       try{
           FileInputStream fis = new FileInputStream(&quot;fileName.txt&quot;);
           ObjectInputStream ois = new ObjectInputStream(fis);
           al = (ArrayList&lt;Person&gt;)ois.readObject();
           ois.close();
           }catch(FileNotFoundException ex){
               System.out.println(&quot;File not found exception&quot;);
           }catch(IOException ex){
               ex.printStackTrace();
           }catch(ClassNotFoundException ex){
              
           }
      
       Object[] array=new Object[al.size()];
       array=al.toArray(array);
      
       for(int i=0;i&lt;=array.length;i++){
           u_city.people[i]=(Person)array[i];
       }
          
       System.out.println(&quot;What would you like to do&quot;);
       System.out.println(&quot;Enter &#39;hire&#39; to hire a new faculty member. &quot;);
       System.out.println(&quot;Enter &#39;admit&#39; to admit a new student &quot;);
       System.out.println(&quot;Enter &#39;find student&#39; to list information about a student&quot;);
       System.out.println(&quot;Enter &#39;find faculty&#39; to list information about a faculty member&quot;);
       System.out.println(&quot;Enter &#39;list students&#39; to list the names of all students.&quot;);
       System.out.println(&quot;Enter &#39;list faculty&#39; to list the names of faculty members&quot;);
       System.out.println(&quot;Enter &#39;quit&#39; to end this program and save data&quot;);
      
       //--------------------&gt;
      
      
       Scanner in =new Scanner(System.in);
       boolean run= true;
       while(run){
          
       String s= in.nextLine();
       if(s.equals(&quot;quit&quot;)){
           run=false;
           //save data in extenal file
       try{
          FileOutputStream fos = new FileOutputStream(&quot;fileName.txt&quot;);
              ObjectOutputStream oos = new ObjectOutputStream(fos);
              oos.writeObject(u_city.people);
              oos.close();
          }catch(IOException ex){
              ex.printStackTrace();
          }
       }
         
       if(s.equals(&quot;hire&quot;)){
           u_city.hire();
       }
       if(s.equals(&quot;admit&quot;)){
           u_city.admit();
       }
       if(s.equals(&quot;find_student&quot;)){
           System.out.println(&quot;What is the student&#39;s first name?&quot;);
           String fn=in.nextLine();
           System.out.println(&quot;What is the student&#39;s last name?&quot;);
           String ln=in.nextLine();
           Student temp=u_city.findStudent(fn, ln);
           if(temp==null){
               System.out.println(&quot;Student not found&quot;);
           }else{
               temp.Details();
           }
       }
       if(s.equals(&quot;find_faculty&quot;)){
           System.out.println(&quot;What is the faculty&#39;s first name?&quot;);
           String fn=in.nextLine();
           System.out.println(&quot;What is the faculty&#39;s last name?&quot;);
           String ln=in.nextLine();
           Faculty temp=u_city.findFaculty(fn, ln);
           if(temp==null){
               System.out.println(&quot;faculty not found&quot;);
           }
       }
      
       if(s.equals(&quot;list_student&quot;)){
           Person[] temp= u_city.getStudents();
           for(int i=0;i&lt;=temp.length;i++){
               System.out.println(temp[i].firstName+&quot; &quot;+temp[i].lastName);
           }
             
       }
       if(s.equals(&quot;list_faculty&quot;)){
           Person[] temp= u_city.getFaculty();
           for(int i=0;i&lt;=temp.length;i++){
               System.out.println(temp[i].firstName+&quot; &quot;+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; //-----------&gt;
      
       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(&quot;Student:&quot;+ this.firstName +&quot; &quot;+ this.lastName);
           System.out.println(&quot;DOB: &quot;+ this.dayBirth+&quot;/&quot;+this.monthBirth+&quot;/&quot;+this.yearBirth);
           System.out.println(&quot;Courses: &quot;);
           for(int i=0;i&lt;=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(&quot;Student:&quot;+ this.firstName +&quot; &quot;+ this.lastName);
           System.out.println(&quot;DOB: &quot;+ this.dayBirth+&quot;/&quot;+this.monthBirth+&quot;/&quot;+this.yearBirth);
           System.out.println(&quot;Major: &quot;+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]= &quot;Hardware Architecture&quot;;
           majors[1]= &quot;Information Analytics&quot;;
           majors[2]= &quot;Quantum Computing&quot;;
           majors[3]= &quot;Undecided&quot;;
          
           courses=new String[12];
           courses[0]=&quot;Computers&quot;;
           courses[1]=&quot;Advance Physics&quot;;
           courses[2]=&quot;Quantum Entanglement&quot;;
           courses[3]=&quot;Parallel Programming&quot;;
           courses[4]=&quot;Advance Algorithums&quot;;
           courses[5]=&quot;FPGA Programming&quot;;
           courses[6]=&quot;Hardware Design&quot;;
           courses[7]=&quot;Embedded Systems&quot;;
           courses[8]=&quot;Signal Processing&quot;;
           courses[9]=&quot;Artificial Intelligence&quot;;
           courses[10]=&quot;Bayesian Logic&quot;;
           courses[11]=&quot;Probablity&quot;;
       }
    
       public Student findStudent(String fn, String ln) {
           // TODO Auto-generated method stub
           for(int i=0;i&lt;=people.length;i++){
           if(people[i].firstName.equals(fn)&amp;&amp; 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&lt;=people.length;i++){
               if(people[i].firstName.equals(fn)&amp;&amp; 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(&quot;You entered string &quot;+s);
          
           System.out.println(&quot;What is the person&#39;s first name?&quot;);
           String f_name= in.nextLine();
           System.out.println(&quot;What is the person&#39;s last name?&quot;);
           String l_name= in.nextLine();
           System.out.println(&quot;What is the person&#39;s month of birth?&quot;);
           int m= in.nextInt();
           System.out.println(&quot;What is the person&#39;s day of birth?&quot;);
           int d= in.nextInt();
           System.out.println(&quot;What is the person&#39;s year of birth?&quot;);
           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(&quot;Assign a course to this Faculty enter &#39;done&#39; if there are no other courses?&quot;);
               String course= in.nextLine();

               if(!course.equals(&quot;done&quot;)) {
                   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(&quot;You entered string &quot;+s);
          
           System.out.println(&quot;What is the person&#39;s first name?&quot;);
           String f_name= in.nextLine();
           System.out.println(&quot;What is the person&#39;s last name?&quot;);
           String l_name= in.nextLine();
           System.out.println(&quot;What is the person&#39;s month of birth?&quot;);
           int m= in.nextInt();
           System.out.println(&quot;What is the person&#39;s day of birth?&quot;);
           int d= in.nextInt();
           System.out.println(&quot;What is the person&#39;s year of birth?&quot;);
           int y= in.nextInt();
           System.out.println(&quot;What is the person&#39;s major?&quot;);
           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&lt;=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&lt;=MAX_SIZE;i++){
               if(people[i].student_or_faculty == false){
                   temp[i]=people[i];
               }
           }
          
           return temp;
       }
    }


</details>


# 答案1
**得分**: 1

将此部分更改为

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

将解决此问题。循环不能超过数组的大小,而且由于索引 i 从 0 开始,最大索引号可以是数组大小减一。

希望这有所帮助。

英文:

Changing this part:

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

to

for (int i = 0; i &lt; 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.

huangapple
  • 本文由 发表于 2020年10月9日 11:17:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64273464.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定