1

The following piece of code is giving me a massive headache. It only kind of does what I want it to do, which is take an inputted string, and then search the arraylist of book objects by the object's classification (a string) and then return that book object. Currently the code below works as long as the input classification is an actual classification of a book in the list. Eg TD999 is found so it is returned. But if I type a bunch of nonsense eg yhgfsdsfbv for the class, it still returns a book eg TD345 (seem it turns a book that was originall classed as null and then classed using the setClassification method when the programme runs.

public Book searchClass(String inputClass){
    Book foundBook = null;
    for(Book findClass : bookStore){
        if(findClass.getClassification()!=null &&
 findClass.getClassification().equalsIgnoreCase(givenClass)) 
        return findClass;
            foundBook = findClass;
    }
    return foundBook;
}

My attempt to fix it was this:

public Book searchClass(String inputClass) throws IllegalArgumentException{

    Book foundBook = null;
    for(Book findClass : bookStore){
        if(!(findClass.getClassification().equalsIgnoreCase(inputClass))){
                   throws new IllegalArgumentException("book not found")
        }
        else if(findClass.getClassification().equalsIgnoreCase(inputClass)){
            foundBook = findClass;
        }
    }
    return foundBook;
}

However this will throw the exception every single time, even if I correctly enter a classification that is in the list.

I have no idea how to fix this to do what I want, I have tired doing it numerous other ways, never works properly. I have read a through all my lecture notes, several textbooks and oracle webpages and have no idea what is causing the problem, hence I am unable to fix it.

4 Answers 4

2

Let's take your first attempt. If you find a book you return it immediately. Therefore, if you get through the entire loop without finding anything, you should signal failure: return null, or throw an exception. There's no point in returning foundBook because you know you didn't find anything.

That means that there's no need for a foundBook variable at all.

public Book searchClass(String inputClass) {
    for (Book book: bookStore) {
        if (book.getClassification() != null &&
            book.getClassification().equalsIgnoreCase(inputClass))
        {
            return book;
        }
    }

    throw new IllegalArgumentException("book not found");
}

Another way to write this is to use Java 8 streams. Instead of an explicit loop you can let a stream take care of looping.

public Book searchClass(String inputClass) {
    return bookStore.stream()
        .filter(book -> book.getClassification() != null)
        .filter(book -> book.getClassification().equalsIgnoreCase(inputClass))
        .findAny()
        .orElseThrow(() -> new IllegalArgumentException("book not found"));
}
Sign up to request clarification or add additional context in comments.

Comments

1

The problem is that, here:

public Book searchClass(String inputClass){
    if(findClass.getClassification()!=null && findClass.getClassification().equalsIgnoreCase(givenClass)) 
    return findClass;
        foundBook = findClass;
}

If the if condition is true, then it returns the book, else, if it is not true, then it sets foundbook to findclass.

Then, in the end, you return foundbook, which will always be returned, even if no book matched.

You must do:

for(Book findClass : bookStore){
    if(findClass.getClassification()!=null && findClass.getClassification().equalsIgnoreCase(givenClass)) 
    return findClass; //return the book
}
return null; // if no book was found, then return null.

Comments

0

I will try to explain on code please read comment and fix your own so it will be useful to make you learn, I hope.

public Book searchClass(String inputClass) throws IllegalArgumentException{

    Book foundBook = null;
    for(Book findClass : bookStore){
       // if(!(findClass.getClassification().equalsIgnoreCase(inputClass))){
         //          throws new IllegalArgumentException("book not found")
        //}//remove these because each loop checks that 
        if(findClass.getClassification().equalsIgnoreCase(inputClass)){
            foundBook = findClass; // return findClass; // you do not need any more iteration you found it.
        }
    }
    return foundBook; //if you are here that means no match throw exception //throws new IllegalArgumentException("book not found")


}

Comments

0
public static int personThereAlready(String name, ArrayList<Person> people) {
    int index = -1;
    for(int i=0; i < people.size();i++) {
        Person person = people.get(i);
            
        if (person.getName().equalsIgnoreCase(name)) {
            index = i;
            break;
        }
    }
    return index;
}

2 Comments

You can use this method that returns the index of the person in a ArrayList of people. If the person that you want is not found, the method returns -1. Useful and simple. I hope I could help! cheers
From Review: Would you please also edit the answer and add and explanation? Thanks!

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.