调用JAVA方法的Return时发生OutOfBoundsException

调用JAVA方法的Return时发生OutOfBoundsException,java,indexoutofboundsexception,Java,Indexoutofboundsexception,我直接去追。如果用户想要读取另一个文件,他们必须在菜单中键入r,然后返回readFile();方法,该方法将他们带到程序顶部,并向他们询问与他们第一次运行此程序时在Begging时相同的问题。唯一的问题是当您键入R或Default时,它会抛出OutOFBoundsException。顺便说一句,它正在读取一个CSV文件 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1000 at studentr

我直接去追。如果用户想要读取另一个文件,他们必须在菜单中键入r,然后返回readFile();方法,该方法将他们带到程序顶部,并向他们询问与他们第一次运行此程序时在Begging时相同的问题。唯一的问题是当您键入R或Default时,它会抛出OutOFBoundsException。顺便说一句,它正在读取一个CSV文件

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1000
    at studentrecs.StudentRecs.in(StudentRecs.java:71)
    at studentrecs.StudentRecs.readFile(StudentRecs.java:55)
    at studentrecs.StudentRecs.menu(StudentRecs.java:97)
    at studentrecs.StudentRecs.main(StudentRecs.java:33)
Java Result: 1
/

publicstaticboolearnreadfile(stringfilename)抛出IOException{//filename的构造函数
试一试{
扫描仪用户输入=新扫描仪(System.in);
System.out.println(“键入R读取文件或键入默认文件的默认值”);
user=userInput.nextLine();
if(user.equalsIgnoreCase(“r”)){
user=userInput.nextLine();
}
filename=用户;
if(user.equalsIgnoreCase(“默认”)){
filename=“newreg2.csv”;
}
扫描仪输入=新扫描仪(新文件读取器(文件名));
while(input.hasNext()){
in(input.nextLine());
numstu++;
}
input.close();
返回true;
}捕获(IOE异常){
System.err.println(e.getMessage());
}
返回false;
}
中的公共静态无效(字符串读取器){
字符串拆分器[];
splitter=reader.split(“,”);
stu[numstu]=新StuRec();
stu[numstu].studentID=splitter[0];
stu[numstu].lastName=splitter[1];
stu[numstu].firstName=splitter[2];
stu[numstu].phoneNumber=splitter[3];
stu[numstu].courseCode=splitter[4];
stu[numstu].periodNumber=Integer.parseInt(拆分器[5]);//parseInt将一串数字转换为整数
stu[numstu].mark=Integer.parseInt(拆分器[6]);
}
公共静态布尔菜单()引发IOException{
字符串选择;
扫描仪用户输入=新扫描仪(System.in);
System.out.println(“======================================================================”);
System.out.println(“键入R读取另一个文件”);
System.out.println(“键入L以打印所有文件记录”);
System.out.println(“键入AA以打印所有标记的平均值”);
System.out.println(“键入X退出程序”);
choice=userInput.nextLine();
双平均值=0.0;//声明平均值
if(选择等信号情况(“L”)){
对于(int i=0;i0){
average=average/numstu;//计算平均值。尽可能使用变量的大小。
System.out.println(average);//如下所述,如果这是一个整数值,<#个学生的计算将求值为0。
}
如果(!choice.equalsIgnoreCase(“AA”)和&numstu<0),则为else{
System.out.println(“哎呀!没有要计算的标记!:(”);
}
返回菜单();
}
}

看起来要么您已将
numstu
初始化为从1开始,要么您的文件中有1000多行

这些错误中的任何一个都会导致您最终尝试将数据写入
stu
的条目1000。但由于您已使用编号为0到999的1000个条目初始化了
stu
,因此会出现错误

您应该确保
numstu
最初是0,而不是1


下次发布问题时,请发布所有代码,而不仅仅是您认为可能出现错误的部分。对于大多数人来说,很难在代码中找到他们看不到的bug。

另一天,另一个没有stacktrace的java索引越界异常问题。在中有一个名为
的方法,您还没有展示我们。你能给我们看一下吗?请给我们看一下stacktrace,以便我们知道哪一行抛出它。你在哪里声明和初始化了
stu
?它有多少个条目?你的文件中有多少行?文件的每一行是否至少有6个逗号,没有空行?在另一个类中,stu已经声明了d、 总共有1000行,6个逗号,没有空格或空白点
     public static Boolean readFile(String filename) throws IOException { //Constructor for filename
        try {
            Scanner userInput = new Scanner(System.in);

            System.out.println("Type R To Read a File or Type Default for the default file");
            user = userInput.nextLine();
            if (user.equalsIgnoreCase("r")) {
                user = userInput.nextLine();
            }
            filename = user;
            if (user.equalsIgnoreCase("default")) {
                filename = "newreg2.csv";
            }

            Scanner input = new Scanner(new FileReader(filename));
            while (input.hasNext()) {
                in(input.nextLine());
                numstu++;
            }
            input.close();
            return true;

        } catch (IOException e) {
            System.err.println(e.getMessage());
        }
        return false;
    }


public static void in(String reader) {
        String splitter[];
        splitter = reader.split(",");
        stu[numstu] = new StuRec();
        stu[numstu].studentID = splitter[0];
        stu[numstu].lastName = splitter[1];
        stu[numstu].firstName = splitter[2];
        stu[numstu].phoneNumber = splitter[3];
        stu[numstu].courseCode = splitter[4];
        stu[numstu].periodNumber = Integer.parseInt(splitter[5]);  // parseInt turns a string of digits into an integer
        stu[numstu].mark = Integer.parseInt(splitter[6]);
    }

 public static boolean menu() throws IOException {
    String choice;
    Scanner userInput = new Scanner(System.in);
    System.out.println("=============================================");
    System.out.println("Type R To Read Another File");
    System.out.println("Type L To Print all File Records");
    System.out.println("Type AA To Print The Average Of All The Marks");
    System.out.println("Type X To Exit The Program");
    choice = userInput.nextLine();
    double average = 0.0; // declare average

        if (choice.equalsIgnoreCase("L")) {
            for (int i = 0; i < numstu; i++) {
           System.out.println(stu[i].lastName + ", " + stu[i].firstName + ", " + stu[i].studentID + ", " + stu[i].phoneNumber + ", " + stu[i].courseCode + ", " + stu[i].periodNumber + ", " + stu[i].mark);
            }
            }else if  (choice.equalsIgnoreCase("R")){
              return readFile(filename);  
        } else if (choice.equalsIgnoreCase("AA")) {
            for (int i = 0; i < numstu; i++) {
              average += stu[i].mark; // keep adding to average

            }
        }else if (choice.equalsIgnoreCase("X")) {
            for (int i = 0; i < numstu; i++) {
                System.exit(i);
            }
        }else if (choice.equalsIgnoreCase("AC"))    {


        } else {System.err.println("Unknown Key Try Again...");  
        }

    // divide by zero protection
    if ( choice.equalsIgnoreCase("AA") && numstu > 0 ) {
        average = average/numstu;  // compute the average. Always use the size in terms of a variable whenever possible.
        System.out.println(average); // as noted below, if this is an integer value, < #of students computations will eval to 0.
    }
    else if (!choice.equalsIgnoreCase("AA") && numstu < 0) {
        System.out.println("Oops! No Marks To Calculate! :(");
    }
    return menu();
}

}