gpt4 book ai didi

线程中的 Java 异常 "main"java.lang.ArrayIndexOutOfBoundsException

转载 作者:行者123 更新时间:2023-12-01 06:36:30 25 4
gpt4 key购买 nike

在为学校开发一个项目时,当我尝试输入数组的学生人数时遇到错误。错误是

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at Project1.enterStudents(Project1.java23)
at Project1.mainMenu(Project1.java59)
at Project1.enterStudents(Project1.java7)

我在下面编写的代码一如既往地感谢任何帮助。

import java.util.Scanner;

public class Project1{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
Project1 project1 = new Project1();
project1.mainMenu();


}//main

int numOfStudents;
Student[] students = new Student[numOfStudents];

public void enterStudents(){
Scanner input = new Scanner(System.in);
System.out.println("Enter number of students");
numOfStudents = input.nextInt();
int i;
for(i = 0; i <= numOfStudents - 1; i++){
i--;
System.out.println("Enter student's ID: ");
students[i].getId();
System.out.println("Enter student's first name: ");
students[i].getFirst();
System.out.println("Enter student's last name: ");
students[i].getLast();
System.out.println("Enter student's class: ");
students[i].getStuClass();
}


}

public void retrieveStuId(){
Scanner input = new Scanner(System.in);
System.out.println("Enter student id");


}

public void Exit(){
System.exit(0);
}

public void mainMenu(){
Scanner input = new Scanner(System.in);
System.out.println("1 - Enter student info");
System.out.println("2 - Retrieve student by ID");
System.out.println("3 - Retrieve student by last name");
System.out.println("4 - Update student");
System.out.println("5 - Exit");
int menuSelect = input.nextInt();

if (menuSelect != 1 && menuSelect != 2 && menuSelect != 3 && menuSelect != 4 && menuSelect != 5)
System.out.println("That is not a option");
else
switch (menuSelect){
case 1: enterStudents();

case 2: System.out.print("case 2");

case 3: System.out.print("case 3");

case 4: System.out.print("case 4");

case 5: Exit();

}
}

}//project1

class Student{
private int studentID;
private String firstName;
private String lastName;
private String stuClass;

public Student(){
}

public Student(int id, String first, String last, String c ){
studentID = id;
firstName = first;
lastName = last;
stuClass = c;
}

public void setID (int id){
studentID = id;
}

public void setStuClass (String c){
stuClass = c;
}

public void setFirst(String first){
firstName = first;
}

public void setLast(String last){
lastName = last;
}

public String getFirst(){
return firstName;
}

public String getLast(){
return lastName;
}

public int getId(){
return studentID;
}

public String getStuClass(){
return stuClass;
}

public String toString(){
return "Student ID: " + studentID + " ---- " + "Student Name: " + firstName + "" + lastName + " ---- " + "Class:" + stuClass;
}


}

最佳答案

看看这段代码:

for(i = 0; i <= numOfStudents - 1; i++){
i--;
System.out.println("Enter student's ID: ");
students[i].getId();

现在计算出每行上 i 的值是多少...

为什么会有 i--; 行?

请注意,这解决了第一个 ArrayIndexOutOfBoundsException 问题 - 一旦修复,您最终将遇到另一个 ArrayIndexOutOfBoundsException 因为您在请求numOfStudents之前初始化数组。

一旦解决这个问题,您将得到一个NullPointerException,因为您试图通过空引用调用方法 - 您实际上从未创建一个新的 Student 实例。

说实话,这个程序距离工作还有很长的路要走 - 我不确定 Stack Overflow 是否会在这种特殊情况下提供最有效的教学环境。我建议你和你的老师谈谈并要求一些一对一的辅导。

关于线程中的 Java 异常 "main"java.lang.ArrayIndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8382266/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com