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

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

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

问题

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

  1. 我在尝试运行我的程序时遇到了这个错误
  2. 有人可以帮我找出问题吗
  3. > 在线程main中的异常java.lang.ArrayIndexOutOfBoundsException索引0超出了长度0的界限
  4. > 位于university_info.UniversityDriver.main(UniversityDriver.java:39)
  5. 我的代码
  6. UniversityDriver.java
  7. university_info;
  8. 导入 java.io.FileInputStream;
  9. 导入 java.io.FileNotFoundException;
  10. 导入 java.io.FileOutputStream;
  11. 导入 java.io.IOException;
  12. 导入 java.io.ObjectInputStream;
  13. 导入 java.io.ObjectOutputStream;
  14. 导入 java.util.ArrayList;
  15. 导入 java.util.Scanner;
  16. 公共类 UniversityDriver {
  17. 公共静态无效 main(String[] args) {
  18. // TODO 自动生成的方法存根
  19. 大学 u_city = new University("hero university","好好教学");
  20. System.out.println("欢迎来到" + u_city.universityName + "大学");
  21. System.out.println(u_city.motto);
  22. // 在启动时从外部文件检索数据...
  23. ArrayList<Person> al = new ArrayList<Person>();
  24. 尝试 {
  25. FileInputStream fis = new FileInputStream("fileName.txt");
  26. ObjectInputStream ois = new ObjectInputStream(fis);
  27. al = (ArrayList<Person>) ois.readObject();
  28. ois.close();
  29. } 捕获 (FileNotFoundException ex) {
  30. System.out.println("文件未找到异常");
  31. } 捕获 (IOException ex) {
  32. ex.printStackTrace();
  33. } 捕获 (ClassNotFoundException ex) {
  34. }
  35. 对象[] 数组 = 新对象[al.size()];
  36. array = al.toArray(array);
  37. 对于 (int i = 0; i <= array.length; i++) {
  38. u_city.people[i] = (Person) array[i];
  39. }
  40. System.out.println("您想要做什么?");
  41. System.out.println("输入'雇佣'以雇佣新的教职员工。 ");
  42. System.out.println("输入'录取'以录取新的学生 ");
  43. System.out.println("输入'查找学生'以列出有关学生的信息");
  44. System.out.println("输入'查找教职员工'以列出有关教职员工的信息");
  45. System.out.println("输入'列出学生'以列出所有学生的姓名。");
  46. System.out.println("输入'列出教职员工'以列出教职员工的姓名");
  47. System.out.println("输入'退出'以结束此程序并保存数据");
  48. //-------------------->
  49. 扫描器 = 新扫描器(System.in);
  50. 布尔值 运行 = ;
  51. (运行) {
  52. 字符串 s = .nextLine();
  53. 如果 (s.equals("退出")) {
  54. 运行 = ;
  55. // 将数据保存在外部文件中
  56. 尝试 {
  57. FileOutputStream fos = new FileOutputStream("fileName.txt");
  58. ObjectOutputStream oos = new ObjectOutputStream(fos);
  59. oos.writeObject(u_city.people);
  60. oos.close();
  61. } 捕获 (IOException ex) {
  62. ex.printStackTrace();
  63. }
  64. }
  65. 如果 (s.equals("雇佣")) {
  66. u_city.hire();
  67. }
  68. 如果 (s.equals("录取")) {
  69. u_city.admit();
  70. }
  71. 如果 (s.equals("查找学生")) {
  72. System.out.println("学生的名字是什么?");
  73. 字符串 fn = .nextLine();
  74. System.out.println("学生的姓氏是什么?");
  75. 字符串 ln = .nextLine();
  76. 学生 temp = u_city.findStudent(fn, ln);
  77. 如果 (temp == null) {
  78. System.out.println("未找到学生");
  79. } 否则 {
  80. temp.Details();
  81. }
  82. }
  83. 如果 (s.equals("查找教职员工")) {
  84. System.out.println("教职员工的名字是什么?");
  85. 字符串 fn = .nextLine();
  86. System.out.println("教职员工的姓氏是什么?");
  87. 字符串 ln = .nextLine();
  88. 教职员工 temp = u_city.findFaculty(fn, ln);
  89. 如果 (temp == null) {
  90. System.out.println("未找到教职员工");
  91. }
  92. }
  93. 如果 (s.equals("列出学生")) {
  94. [] temp = u_city.getStudents();
  95. 对于 (int i = 0; i <= temp.length; i++) {
  96. System.out.println(temp[i].firstName + " " + temp[i].lastName);
  97. }
  98. }
  99. 如果 (s.equals("列出教职员工")) {
  100. [] temp = u_city.getFaculty();
  101. 对于 (int i = 0; i <= temp.length; i++) {
  102. System.out.println(temp[i].firstName + " " + temp[i].lastName);
  103. }
  104. }
  105. }
  106. }
  107. }
  108. Faculty.java
  109. university_info;
  110. 公共类 Faculty 扩展 Person {
  111. 字符串[] 课程; // 教职员工教授的课程
  112. 公共教职员工(String first, String last, int m, int d, int y, String[] course, 布尔值 bol) {
  113. 这个.课程 = 课程; //----------->
  114. 这个.firstName = first;
  115. 这个.lastName = last;
  116. 这个.monthBirth = m;
  117. 这个.dayBirth = d;
  118. 这个.yearBirth = y;
  119. 这个.student_or_faculty = bol;
  120. }
  121. 公共无效 详情_教职员工() {
  122. System.out.println("学生:" + 这个.firstName + " " + 这个.lastName);
  123. System.out.println("出生日期: " + 这个.dayBirth + "/" + 这个.monthBirth + "/" + 这个.yearBirth);
  124. System.out.println("课程: ");
  125. 对于 (int i = 0; i <= 课程.length; i++) {
  126. System.out.println(课程[i]);
  127. }
  128. }
  129. }
  130. Person.java
  131. university_info;
  132. 公共类 Person {
  133. 字符串 firstName;
  134. 字符串 lastName;
  135. 整数 monthBirth;
  136. 整数 dayBirth;
  137. 整数 yearBirth;
  138. 布尔值 student_or_faculty; // 学生为真,教职员工为假
  139. 公共人() {} //
  140. <details>
  141. <summary>英文:</summary>
  142. I am getting this error when trying to run my program.
  143. Can someone help me figure it out?
  144. &gt; Exception in thread &quot;main&quot; java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
  145. &gt; at university_info.UniversityDriver.main(UniversityDriver.java:39)
  146. My code:
  147. UniversityDriver.java
  148. package university_info;
  149. import java.io.FileInputStream;
  150. import java.io.FileNotFoundException;
  151. import java.io.FileOutputStream;
  152. import java.io.IOException;
  153. import java.io.ObjectInputStream;
  154. import java.io.ObjectOutputStream;
  155. import java.util.ArrayList;
  156. import java.util.Scanner;
  157. public class UniversityDriver{
  158. public static void main(String[] args) {
  159. // TODO Auto-generated method stub
  160. University u_city=new University(&quot;hero university&quot;,&quot;Teach well&quot;);
  161. System.out.println(&quot;Welcome to &quot;+u_city.universityName+&quot; Univercity&quot;);
  162. System.out.println(u_city.motto);
  163. // retrieving data from external file on startup.....
  164. ArrayList&lt;Person&gt; al=new ArrayList&lt;Person&gt;();
  165. try{
  166. FileInputStream fis = new FileInputStream(&quot;fileName.txt&quot;);
  167. ObjectInputStream ois = new ObjectInputStream(fis);
  168. al = (ArrayList&lt;Person&gt;)ois.readObject();
  169. ois.close();
  170. }catch(FileNotFoundException ex){
  171. System.out.println(&quot;File not found exception&quot;);
  172. }catch(IOException ex){
  173. ex.printStackTrace();
  174. }catch(ClassNotFoundException ex){
  175. }
  176. Object[] array=new Object[al.size()];
  177. array=al.toArray(array);
  178. for(int i=0;i&lt;=array.length;i++){
  179. u_city.people[i]=(Person)array[i];
  180. }
  181. System.out.println(&quot;What would you like to do&quot;);
  182. System.out.println(&quot;Enter &#39;hire&#39; to hire a new faculty member. &quot;);
  183. System.out.println(&quot;Enter &#39;admit&#39; to admit a new student &quot;);
  184. System.out.println(&quot;Enter &#39;find student&#39; to list information about a student&quot;);
  185. System.out.println(&quot;Enter &#39;find faculty&#39; to list information about a faculty member&quot;);
  186. System.out.println(&quot;Enter &#39;list students&#39; to list the names of all students.&quot;);
  187. System.out.println(&quot;Enter &#39;list faculty&#39; to list the names of faculty members&quot;);
  188. System.out.println(&quot;Enter &#39;quit&#39; to end this program and save data&quot;);
  189. //--------------------&gt;
  190. Scanner in =new Scanner(System.in);
  191. boolean run= true;
  192. while(run){
  193. String s= in.nextLine();
  194. if(s.equals(&quot;quit&quot;)){
  195. run=false;
  196. //save data in extenal file
  197. try{
  198. FileOutputStream fos = new FileOutputStream(&quot;fileName.txt&quot;);
  199. ObjectOutputStream oos = new ObjectOutputStream(fos);
  200. oos.writeObject(u_city.people);
  201. oos.close();
  202. }catch(IOException ex){
  203. ex.printStackTrace();
  204. }
  205. }
  206. if(s.equals(&quot;hire&quot;)){
  207. u_city.hire();
  208. }
  209. if(s.equals(&quot;admit&quot;)){
  210. u_city.admit();
  211. }
  212. if(s.equals(&quot;find_student&quot;)){
  213. System.out.println(&quot;What is the student&#39;s first name?&quot;);
  214. String fn=in.nextLine();
  215. System.out.println(&quot;What is the student&#39;s last name?&quot;);
  216. String ln=in.nextLine();
  217. Student temp=u_city.findStudent(fn, ln);
  218. if(temp==null){
  219. System.out.println(&quot;Student not found&quot;);
  220. }else{
  221. temp.Details();
  222. }
  223. }
  224. if(s.equals(&quot;find_faculty&quot;)){
  225. System.out.println(&quot;What is the faculty&#39;s first name?&quot;);
  226. String fn=in.nextLine();
  227. System.out.println(&quot;What is the faculty&#39;s last name?&quot;);
  228. String ln=in.nextLine();
  229. Faculty temp=u_city.findFaculty(fn, ln);
  230. if(temp==null){
  231. System.out.println(&quot;faculty not found&quot;);
  232. }
  233. }
  234. if(s.equals(&quot;list_student&quot;)){
  235. Person[] temp= u_city.getStudents();
  236. for(int i=0;i&lt;=temp.length;i++){
  237. System.out.println(temp[i].firstName+&quot; &quot;+temp[i].lastName);
  238. }
  239. }
  240. if(s.equals(&quot;list_faculty&quot;)){
  241. Person[] temp= u_city.getFaculty();
  242. for(int i=0;i&lt;=temp.length;i++){
  243. System.out.println(temp[i].firstName+&quot; &quot;+temp[i].lastName);
  244. }
  245. }
  246. }
  247. }
  248. }
  249. Faculty.java
  250. package university_info;
  251. public class Faculty extends Person {
  252. String[] courses; // courses that faculty member teaches
  253. public Faculty(String first,String last,int m,int d,int y,String[] course,boolean bol){
  254. this.courses=course; //-----------&gt;
  255. this.firstName=first;
  256. this.lastName=last;
  257. this.monthBirth=m;
  258. this.dayBirth=d;
  259. this.yearBirth=y;
  260. this.student_or_faculty=bol;
  261. }
  262. public void Details_Faculty(){
  263. System.out.println(&quot;Student:&quot;+ this.firstName +&quot; &quot;+ this.lastName);
  264. System.out.println(&quot;DOB: &quot;+ this.dayBirth+&quot;/&quot;+this.monthBirth+&quot;/&quot;+this.yearBirth);
  265. System.out.println(&quot;Courses: &quot;);
  266. for(int i=0;i&lt;=courses.length;i++){
  267. System.out.println(courses[i]);
  268. }
  269. }
  270. }
  271. Person.java
  272. package university_info;
  273. public class Person {
  274. String firstName;
  275. String lastName;
  276. int monthBirth;
  277. int dayBirth;
  278. int yearBirth;
  279. boolean student_or_faculty; // true for student and false for faculty
  280. public Person(){} //must have a constructor
  281. }
  282. School.java
  283. package university_info;
  284. public interface School {
  285. Student findStudent(String fn,String ln);
  286. Faculty findFaculty(String fn,String ln);
  287. Faculty hire();
  288. Student admit();
  289. Person[] getAllPersons();
  290. String[] getAllMajors();
  291. String[] getAllCourses();
  292. Person[] getStudents();
  293. Person[] getFaculty();
  294. }
  295. Student.java
  296. package university_info;
  297. public class Student extends Person {
  298. String major;
  299. public Student(String first,String last,int m,int d,int y,String major,boolean bol){
  300. this.major=major;
  301. this.firstName=first;
  302. this.lastName=last;
  303. this.monthBirth=m;
  304. this.dayBirth=d;
  305. this.yearBirth=y;
  306. this.student_or_faculty=bol;
  307. } // must have a constructor
  308. public void Details(){
  309. System.out.println(&quot;Student:&quot;+ this.firstName +&quot; &quot;+ this.lastName);
  310. System.out.println(&quot;DOB: &quot;+ this.dayBirth+&quot;/&quot;+this.monthBirth+&quot;/&quot;+this.yearBirth);
  311. System.out.println(&quot;Major: &quot;+this.major);
  312. }
  313. }
  314. University.java
  315. package university_info;
  316. import java.util.Scanner;
  317. public class University implements School {
  318. int MAX_SIZE= 500;
  319. String universityName;
  320. String motto;
  321. Person[] people;
  322. String[] majors;
  323. String[] courses;
  324. public University(String u_Name,String u_motto){
  325. this.universityName=u_Name;
  326. this.motto=u_motto;
  327. people=new Person[MAX_SIZE];
  328. majors=new String[4];
  329. majors[0]= &quot;Hardware Architecture&quot;;
  330. majors[1]= &quot;Information Analytics&quot;;
  331. majors[2]= &quot;Quantum Computing&quot;;
  332. majors[3]= &quot;Undecided&quot;;
  333. courses=new String[12];
  334. courses[0]=&quot;Computers&quot;;
  335. courses[1]=&quot;Advance Physics&quot;;
  336. courses[2]=&quot;Quantum Entanglement&quot;;
  337. courses[3]=&quot;Parallel Programming&quot;;
  338. courses[4]=&quot;Advance Algorithums&quot;;
  339. courses[5]=&quot;FPGA Programming&quot;;
  340. courses[6]=&quot;Hardware Design&quot;;
  341. courses[7]=&quot;Embedded Systems&quot;;
  342. courses[8]=&quot;Signal Processing&quot;;
  343. courses[9]=&quot;Artificial Intelligence&quot;;
  344. courses[10]=&quot;Bayesian Logic&quot;;
  345. courses[11]=&quot;Probablity&quot;;
  346. }
  347. public Student findStudent(String fn, String ln) {
  348. // TODO Auto-generated method stub
  349. for(int i=0;i&lt;=people.length;i++){
  350. if(people[i].firstName.equals(fn)&amp;&amp; people[i].lastName.equals(ln));
  351. return (Student)people[i];
  352. }
  353. return null;
  354. }
  355. public Faculty findFaculty(String fn, String ln) {
  356. // TODO Auto-generated method stub
  357. for(int i=0;i&lt;=people.length;i++){
  358. if(people[i].firstName.equals(fn)&amp;&amp; people[i].lastName.equals(ln));
  359. return (Faculty)people[i];
  360. }
  361. return null;
  362. }
  363. public Faculty hire() {
  364. // TODO Auto-generated method stub
  365. Scanner in =new Scanner(System.in);
  366. String s= in.nextLine();
  367. System.out.println(&quot;You entered string &quot;+s);
  368. System.out.println(&quot;What is the person&#39;s first name?&quot;);
  369. String f_name= in.nextLine();
  370. System.out.println(&quot;What is the person&#39;s last name?&quot;);
  371. String l_name= in.nextLine();
  372. System.out.println(&quot;What is the person&#39;s month of birth?&quot;);
  373. int m= in.nextInt();
  374. System.out.println(&quot;What is the person&#39;s day of birth?&quot;);
  375. int d= in.nextInt();
  376. System.out.println(&quot;What is the person&#39;s year of birth?&quot;);
  377. int y= in.nextInt();
  378. String[] c=new String[20];// max 20 courses can be assigned to faculty
  379. int i=0;
  380. boolean condition=true;
  381. while(condition) {
  382. System.out.println(&quot;Assign a course to this Faculty enter &#39;done&#39; if there are no other courses?&quot;);
  383. String course= in.nextLine();
  384. if(!course.equals(&quot;done&quot;)) {
  385. c[i]=course;
  386. i++;
  387. }
  388. condition=false;
  389. }
  390. boolean b=false;
  391. Faculty temp=new Faculty(f_name,l_name,m,d,y,c,b);
  392. return temp;
  393. }
  394. public Student admit() {
  395. // TODO Auto-generated method stub
  396. Scanner in =new Scanner(System.in);
  397. String s= in.nextLine();
  398. System.out.println(&quot;You entered string &quot;+s);
  399. System.out.println(&quot;What is the person&#39;s first name?&quot;);
  400. String f_name= in.nextLine();
  401. System.out.println(&quot;What is the person&#39;s last name?&quot;);
  402. String l_name= in.nextLine();
  403. System.out.println(&quot;What is the person&#39;s month of birth?&quot;);
  404. int m= in.nextInt();
  405. System.out.println(&quot;What is the person&#39;s day of birth?&quot;);
  406. int d= in.nextInt();
  407. System.out.println(&quot;What is the person&#39;s year of birth?&quot;);
  408. int y= in.nextInt();
  409. System.out.println(&quot;What is the person&#39;s major?&quot;);
  410. String major= in.nextLine();
  411. boolean b=true;
  412. Student temp=new Student(f_name,l_name,m,d,y,major,b);
  413. return temp;
  414. }
  415. public Person[] getAllPersons() {
  416. // TODO Auto-generated method stub
  417. return this.people;
  418. }
  419. public String[] getAllMajors() {
  420. // TODO Auto-generated method stub
  421. return this.majors;
  422. }
  423. public String[] getAllCourses() {
  424. // TODO Auto-generated method stub
  425. return this.courses;
  426. }
  427. public Person[] getStudents() {
  428. // TODO Auto-generated method stub
  429. Person[] temp=new Person[MAX_SIZE];
  430. for(int i=0;i&lt;=MAX_SIZE;i++){
  431. if(people[i].student_or_faculty == true){
  432. temp[i]=people[i];
  433. }
  434. }
  435. return temp;
  436. }
  437. public Person[] getFaculty() {
  438. // TODO Auto-generated method stub
  439. Person[] temp=new Person[MAX_SIZE];
  440. for(int i=0;i&lt;=MAX_SIZE;i++){
  441. if(people[i].student_or_faculty == false){
  442. temp[i]=people[i];
  443. }
  444. }
  445. return temp;
  446. }
  447. }
  448. </details>
  449. # 答案1
  450. **得分**: 1
  451. 将此部分更改为
  452. ```java
  453. for (int i = 0; i < array.length; i++) {
  454. u_city.people[i] = (Person) array[i];
  455. }

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

希望这有所帮助。

英文:

Changing this part:

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

to

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

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:

确定