0

I created a method which searches for a string in an array list, however I the problem is, if I search for a book it has to be the exactly how it was inputted into the array list.

e.g if I type in "Harry Potter" as a book title and search for it, I have to put in "Harry Potter", I can't put "harry potter" or "HARRY POTTER"as it won't recognize it, i know there is something called ignore case but can't get it to work, or may have had it in the wrong place.

My code:

public void searchBook() {

    System.out.println("\n"+"Enter the title of the book you would like to search for: ");
    String search = input.nextLine();

    for (int i = 0; i < newBook.size(); i++) {

        // IF statement to check that any book in the array list equals what
        // the user has typed in
        if ( newBook.get(i).getTitle().equals(search)  ) {

            System.out.println(("Book ID: " + newBook.get(i).getBookID() + "  Title: " + newBook.get(i).getTitle()
                    + "    Author: " + newBook.get(i).getAuthor() + "  Genre: " + newBook.get(i).getGenre()
                    + "   Date registered: " + newBook.get(i).getDateReg() + "   Loan Status: "
                    + newBook.get(i).getLoan() + "    Number of times loaned: " + newBook.get(i).getNumOfLoans()));

        } else {

            System.out.println("No match was found");
        }

    } // end of for

}// end of method
3
  • 2
    Please try if ( newBook.get(i).getTitle().toLowerCase().contains(search.toLowerCase()) ). This way is more correct Commented Dec 9, 2015 at 12:24
  • Rather than calling newBook.get(i) a ton of times, call it once and store it in a local variable. Commented Dec 9, 2015 at 12:27
  • You should go with contains method and lowercase conversion as Tuna said. Commented Dec 10, 2015 at 5:20

3 Answers 3

3

Change

if (newBook.get(i).getTitle().equals(search)

to

if (newBook.get(i).getTitle().equalsIgnoreCase(search)

The equalsIgnoreCase performs a String comparison ignoring case.

Sign up to request clarification or add additional context in comments.

Comments

1

use equalsIgnoreCase() instead of equals()

Read for documentation here

from javadoc,

Compares this String to another String, ignoring case considerations. Two strings are considered equal ignoring case if they are of the same length and corresponding characters in the two strings are equal ignoring case.

Comments

0

For searching contains method is more correct

if (newBook.get(i).getTitle().toLowerCase().contains(search.toLowerCase()))

5 Comments

Explain how it is more correct than equalsignorecase method, and how you consider contains over equalsignorecase?
equalsIgnoreCase(String) already does its own case conversion operations internally and is much easier to read.
Dear friends. we all know what equalsIgnoreCase() is. But he is inputting a text. The text may be "rry" then he couldnt find any answer for him.
Well, I support @Tuna Karakasoglu for this case. This kind of implementation will cover more cases and return more legit result. I dont know why people down-voted this answer.
Well, read question again, but sorry cannot vote up again locked. i am with this answer.

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.