简体   繁体   English

如何解决此“ java.lang.IndexOutOfBoundsException”错误?

[英]How to solve this “java.lang.IndexOutOfBoundsException” error?

Currently I am writing the code for a library application which stores manuals and allows users to borrow said manuals out for a certain period of time. 目前,我正在为图书馆应用程序编写代码,该应用程序存储手册,并允许用户在一定时期内借用这些手册。

I am almost complete with the application however I have run into a confusing problem which I am unable to solve. 我几乎完成了该应用程序,但是遇到了一个无法解决的令人困惑的问题。

When the user selects to borrow a manual from the library, they are asked to enter an index number which states which book in the library to borrow, each manual can be borrowed successfully and if an incorrect index number is entered they are displayed with an "error" message. 当用户选择从图书馆借书时,要求他们输入一个索引号,指出要借阅图书馆中的哪本书籍,每本手册都可以成功借入,如果输入的索引号不正确,则显示“错误信息。

However, if the user enters an index number which is equal to the amount of manuals present in the library, the application cuts off and displays the following error: 但是,如果用户输入的索引号等于库中的手册数量,则该应用程序将中断并显示以下错误:

在此处输入图片说明

This error occurred once 2 manuals had been stored in the library, the user then entered "2" as the index number. 一旦将2本手册存储在库中,就会发生此错误,然后用户输入“ 2”作为索引号。

Here is the code I am currently working with, if anyone can tell me what is causing this error: 这是我当前正在使用的代码,如果有人可以告诉我是什么导致了此错误:

public static void borrowManual(){
    displayManualList();

    //register user's Manual choice.
    ManualChoice = (Console.readInteger(Messages.enterManualIndexMessage, Messages.ManualIndexNotInListMessage, 0, Library.ManualList.size()));

    borrowLoop:
    while(Menu.menuChoice == 3){
        //Check if the Manual to be borrowed is available.
        //ManualChoice = (Console.readInteger(Messages.enterManualIndexMessage, Messages.ManualIndexNotInListMessage, 1, Library.ManualList.size()));

        if ((ManualList.get(ManualChoice).status.equalsIgnoreCase(status1)) && (ManualList.size() >= ManualChoice)){
            //Print the borrowed Manual information and change the Manual status to borrowed.
            ManualList.get(ManualChoice).status = "Borrowed";
            ManualList.get(ManualChoice).borrower = User.userName;
            ManualList.get(ManualChoice).borrowDate = "Today.";
            ManualList.get(ManualChoice).returnDate = "In two weeks.";
            //Add the borrowed Manual to the borrowedManuals arraylist:
            borrowedManuals.add(ManualList.get(ManualChoice));

            System.out.printf("\n==========================================================================\n");
            System.out.printf("\n\nYou have chosen the following Manual:\n\n %s\n\n", ManualList.get(ManualChoice).displayManual());
            System.out.println("Please return the Manual within two weeks!\n");
            System.out.println("\n--------------------------------------------------------------------------");
            System.out.println("\n                             Manual borrowed!\n");
            System.out.println("--------------------------------------------------------------------------\n");
            break borrowLoop;

        }else if(ManualList.get(ManualChoice).status.equalsIgnoreCase(status2) && ManualList.size() >= ManualChoice){
            System.out.println("\n\n--------------------------------------------------------------------------");
            System.out.println("\nError! The manual you wish to borrow is already on loan.");
            System.out.println("\n--------------------------------------------------------------------------\n");
            break borrowLoop;

        }else if(ManualChoice > ManualList.size()-1){
            System.out.println(Messages.noSuchManualMessage);
            break borrowLoop;
        }
    }
    Menu.displayMenu();
}

If I need to include more of my code from other classes please let me know as I am relatively new to programming. 如果我需要包括其他类的更多代码,请告诉我,因为我是编程的新手。

When the ArrayList has 2 elements (ie its size() is 2), the valid indices are 0 and 1 . ArrayList具有2个元素(即,其size()为2)时,有效索引为01 2 is out of bounds. 2超出范围。

ManualChoice must be < ManualList.size() . ManualChoice必须为< ManualList.size() It can't be equal to it. 它不能等于它。

You should probably change 你应该改变

ManualChoice = (Console.readInteger(Messages.enterManualIndexMessage, Messages.ManualIndexNotInListMessage, 0, Library.ManualList.size()));

to

ManualChoice = (Console.readInteger(Messages.enterManualIndexMessage, Messages.ManualIndexNotInListMessage, 0, Library.ManualList.size() - 1));

When using such things as ArrayLists and tables, one must remember that computers start counting at 0, not 1. So if an array list has a size of 4, its indexes go 0,1,2,3. 当使用诸如ArrayLists和table之类的东西时,必须记住计算机从0开始计数,而不是从1开始计数。因此,如果数组列表的大小为4,则其索引为0、1、2、3。 What you can do is check to see if the index the user is imputing is a valid input before you let them check out the manual. 您可以做的是在让他们签出手册之前,检查用户输入的索引是否为有效输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM