在university_info处,索引0超出了长度0的界限.UniversityDriver.main(Univers



我在尝试运行程序时遇到此错误。

有人能帮我弄清楚吗?

线程中的异常"主";java.lang.ArrayIndexOutOfBoundsException:在university_info处,索引0超出了长度为0的界限
。UniversityDriver.main(UniversityDriver.java:39(

我的代码:

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);
}
}

大学.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;
}
}

更改此部分:

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

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

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

希望这能帮上忙。

相关内容

  • 没有找到相关文章

最新更新