-1

Currently I am working on a application which allows technical manuals to be added, stored, borrowed, returned to a virtual library.

Currently I am working on the section which allows the user to return a book they have previously borrowed. When they enter "4" at the main menu they will be displayed with a list of the currently borrowed manuals.

I have managed to get the borrowed manuals to display but I am also met with an error which quits my java application, shown here:

enter image description here

Here is the relevant code for this application (if I need to display more please let me know):

static void returnManual(){
    System.out.printf("\n\nHere are the Manual/s currently out on loan:\n\n");
    if(ManualList.get(ManualChoice).status.equalsIgnoreCase(status2) && borrowedManuals.size() >= ManualChoice){
    for (int i = 0; i < borrowedManuals.size(); i++)
        System.out.println(borrowedManuals.get(i).displayManual());
        returnManualTitle = Console.readString(Messages.enterManualTitle, Messages.tooShortMessage, 3);
    }

If anyone knows how this error can be resolved please let me know :) I am fairly new to Java!


UPDATE

I have applied the Christophers suggestion made in the comments to change ManualList.size() to borrowedManuals.size().

The issue now is that if the user borrows 2 manuals and wishes to return the second manual, their title entry is not activated once they enter the title name for the (second) manual they wish to return. If the first manual title is entered, the application functions as expected and returns the manual to the library.

Below is an example of this. I have also updated my code (above) to include the changes made.

enter image description here

Oscar
  • 511
  • 2
  • 10
  • 25
  • 2
    Are you sure `ManualList` and `borrowedManuals` have the same size? – Daniel Cheng Jan 03 '15 at 14:59
  • 2
    Why are you using ManualList.size() and not borrowedManuals.size()? You are trying to access the third element in a list that only has 2 elements. (Index of 2 means the 3rd element since it starts at 0) – cshannon Jan 03 '15 at 14:59
  • 2
    Also, don't name variables/fields with an initial capital. The convention is lowercase initial for fields and variables, uppercase first initial for class names. It confuses code coloring and makes your code less readable. – RealSkeptic Jan 03 '15 at 15:05
  • @ChristopherShannnon thanks for your reply, I have implemented your suggestions and updated my answer with a new issue which occurs – Oscar Jan 03 '15 at 16:03
  • 1
    Yo ushould not change your question from one topic to the other. The index out of bounds is fixed, so open a new question for your new problem. – eckes Jan 03 '15 at 17:28

1 Answers1

1
if(null != ManualList && ManualList.size() >= ManualChoice &&
          ManualList.get(ManualChoice).status.equalsIgnoreCase(status2) ){
for (int i = 0; i < ManualList.size(); i++)
    if(borrowedManuals.size() > i)  // check borrowedManuals size its better to check this in if where you check for ManualList 
       System.out.println(borrowedManuals.get(i).displayManual());
       returnManualTitle = Console.readString(Messages.enterManualTitle, Messages.tooShortMessage, 3);
    }
 }

First check ManualList is not null then check ManualList.size() >= ManualChoice ManualList size is greater then ManualChoice then get the value from that index so it will not give you IndexOutOfBoundException

atish shimpi
  • 4,873
  • 2
  • 32
  • 50
  • Hmm, this seems to only display 1 borrowed manual, even if the user has borrowed several. Do you know why this is happening? Also, once the user enters the title for the 1 manual shown, the next manual they have borrowed is displayed. – Oscar Jan 03 '15 at 15:51
  • check size of `borrowedManuals` list, I think you have only one item in this list – atish shimpi Jan 03 '15 at 16:12