2015-01-02 422 views
0

目前,我正在編寫存儲手冊的圖書館應用程序的代碼,並允許用戶借用所述手冊一段時間。如何解決這個「java.lang.IndexOutOfBoundsException」錯誤?

我幾乎完整的應用程序,但我遇到了一個令人困惑的問題,我無法解決。

當用戶選擇從圖書館借用手冊時,系統會要求他們輸入一個索引號,指出圖書館借用哪本書,每本手冊都可以成功借用,如果輸入的索引號不正確,顯示「錯誤」消息。

然而,如果用戶輸入的索引號等於存在於該文庫手冊的量,應用切斷並顯示以下錯誤:

enter image description here

發生一次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(); 
} 

如果我需要包括更多的從其他類我的代碼,請讓我知道,因爲我在編程方面相對較新。

回答

1

ArrayList有2個元素(即其size()爲2)時,有效索引是012超出界限。

ManualChoice必須是< ManualList.size()。它不能等於它。

你或許應該改變

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

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

當使用這樣的事情的ArrayList和表格,一個必須記住,電腦從0開始計數,而不是1所以,如果一個數組列表大小爲4,其索引爲0,1,2,3。您可以執行的操作是在讓用戶查看手冊之前,檢查用戶輸入的索引是否是有效的輸入。