为方法JAVA调用返回时的OutOfBoundsException

时间:2013-11-21 01:13:06

标签: java indexoutofboundsexception

我会直接追逐。如果用户想要读取另一个文件,则必须在菜单中键入r,然后返回它们返回readFile();将它们带到程序顶部的方法,并询问它们在第一次运行此程序时在初始化时所做的相同问题。唯一的问题是当您键入R或Default时会抛出OutOFBoundsException。 BTW它正在读取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

/

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

}

1 个答案:

答案 0 :(得分:0)

看起来你已初始化numstu从1开始,或者你的文件中有超过1000行。

这些错误中的任何一个的影响都是您最终尝试将数据写入stu的条目1000。但是,由于你已经用1000个条目初始化stu,编号从0到999,这就是你的错误。

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

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