目前,我正在編寫存儲手冊的圖書館應用程序的代碼,並允許用戶借用所述手冊一段時間。如何解決這個「java.lang.IndexOutOfBoundsException」錯誤?
我幾乎完整的應用程序,但我遇到了一個令人困惑的問題,我無法解決。
當用戶選擇從圖書館借用手冊時,系統會要求他們輸入一個索引號,指出圖書館借用哪本書,每本手冊都可以成功借用,如果輸入的索引號不正確,顯示「錯誤」消息。
然而,如果用戶輸入的索引號等於存在於該文庫手冊的量,應用切斷並顯示以下錯誤:
發生一次2此錯誤手冊已存儲在庫中,然後用戶輸入「2」作爲索引號。
這是我目前使用的代碼,如果有人能告訴我是什麼原因造成這樣的錯誤:
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();
}
如果我需要包括更多的從其他類我的代碼,請讓我知道,因爲我在編程方面相對較新。