0

Hi I am encountering a problem with a uni project I am working on. I am trying to validate the input so that the BookID that is entered when attempting to loan the book, is only valid if it exists in the array named 'BookList'. At the minute I have it working so that it validates it to make sure that an integer is entered, and not letters or negative numbers.

I have tried endlessly but I am stuck completely?? any tips or help, I would much appreciate it. thanks

//loan a book method
            public void loanBook() {
                int loanID;
                do {
                    System.out.println("Please enter the Book ID of the book that you wish to borrow");
                    while (!input.hasNextInt()) { // checking that the ID entered is an integer - validation
                        System.out.println("That is not an integer");
                        input.nextLine(); //pushing the scanner on
                    }
                    loanID = input.nextInt(); //setting the loanID variable equal to the input from the scanner.
                }
                while (loanID < 0 || loanID > 100000000); //VALIDATION - NEED TO CHANGE SO THAT WHILE LOAN ID EXISTS IN ARRAY LIST ????
                for (int i = 0; i < BookList.size(); i++) { //for loop to go through and check for the ID entered to remove the book that it corresponds to
                    if (BookList.get(i).getBookID() == loanID ) {
                        System.out.println("The book named : " + BookList.get(i).getTitle() + " has now been taken out on loan. Please return within 2 weeks!");
                        BookList.get(i).setStatus("On Loan");;
                    }//end of if statement

                }//end of for loop

            } //end of return book method
3
  • So whats the problem? Commented Dec 9, 2015 at 16:28
  • on line 11 I have it currently validating that the number entered must be between 0 and 100000000. I need to change this so that the number entered must be an element in my array list named BookList, so I can produce an error saying "This book does not exist in the Library." Commented Dec 9, 2015 at 16:33
  • use input.NextInt() direclty and guard the block with InputMismatchException. When this exception is caught , you have input anything else but Integer. If not thrown, you have your integer now . Use it wherever/however you want like to loop throught your book list id Commented Dec 9, 2015 at 16:34

2 Answers 2

2

You can use the .contains() method for Arraylists. You just need to make sure you are removing items depending on their status.

if(bookList.contains(loanID)){
   //logic for book exists
}else{
   //book is on loan.
}

Now as I said you need to make sure you are doing proper verification for removing of the books on loan etc for this to work. The way you have your logic right now is doing a LOT of unnecessary work with your loops. This way you can easily scan the list and find the item needed. Of course there are better ways to set up your lists etc but this should work keeping your code very similar.

EDIT

You requested information on how you would find the index of the item after you have verified it exists. This is still very simple. Once you have verified that the item exists you would use the line:

int index = bookList.indexOf(loanID);

This will return the index in your ArrayList for the location of the book. Once you have the index you can begin doing everything you were doing before with:

bookList.get(index).getBookId();

or

bookList.get(bookList.indexOf(itemId)).getBookId();

This is almost exactly what you were doing previously but cut down to 3 lines and can be made even shorter.

if (BookList.contains(loanID)) {
     int index = BookList.indexOf(loanId);
     if (!BookList.get(index).getStatus().equals("On Loan")) {
         System.out.println("The book named: " + BookList.get(index).getTitle() + " has now been taken on loan.");
         BookList.get(index).setStatus("On Loan.");
     }else{
         System.out.println("Book is on loan already.");
     }
}else{
    //logic for not existing.
}
Sign up to request clarification or add additional context in comments.

2 Comments

so how would you manipulate the book that you found to exist in the array list? ie use the mutators associated with it, as at the mintue I am using a loop so i can use get(i) to manipulate it. thanks
Literally right below your initial validation but I don't want to write all of the code for you. Try throwing it in there first and then see what happens :) That is the fun of coding.
0

Create a variable int isExist = 0; After getting the input from user...go through the array and see if that book exists. Then make isExist=1; And then of the loop just make if statement

if( isExist == 0) { 
System.out.println("Book is not found");
}

By the way once you have found the book in the array you want to break out of the loop using break;

1 Comment

Why use an int when you could use a boolean value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.