#Removing a Specific Object from an ArrayList by Property Value in Java

1 messages · Page 1 of 1 (latest)

timid hemlock
#

I am working with an ArrayList that contains multiple objects. I would like to remove a specific object from the list. However, I do not know the index of this object; instead, I only know the value of one of its properties (for example, its ID or name). What is the correct or recommended way to remove such an object from the ArrayList in Java?

I tried to iterate over the ArrayList and remove the book when its ISBN matches the target, like this:

while (searchBook) {
            System.out.print("Enter the ISBN (10 or 13 digits) of the book to remove: ");
            bookISBN = scan.nextLine().trim();
            if (bookISBN.length() != 10 && bookISBN.length() != 13) {
                System.out.println("[!] Invalid ISBN. It must contain exactly 10 or 13 digits.");
            }

            Book suchBook = new Book(null, null, bookISBN);
            for (Book book : books) {
                if (suchBook.getIsbn(suchBook).equals(bookISBN)) {
                    books.remove(new Book(null, null, bookISBN));
                    searchBook = false;
                }
            }
            System.out.println("[!] No book found with the given ISBN.");
        }
brazen anvilBOT
# timid hemlock I am working with an ArrayList that contains multiple objects. I would like to r...

Detected code, here are some useful tools:

[WARNING] The code couldn't end properly...

Problematic source code:

while (searchBook) {
            System.out.print("Enter the ISBN (10 or 13 digits) of the book to remove: ");
            bookISBN = scan.nextLine().trim();
            if (bookISBN.length() != 10 && bookISBN.length() != 13) {
                System.out.println("[!] Invalid ISBN. It must contain exactly 10 or 13 digits.");
            }

            Book suchBook = new Book(null, null, bookISBN);
            for (Book book : books) {
                if (suchBook.getIsbn(suchBook).equals(bookISBN)) {
                    books.remove(new Book(null, null, bookISBN));
                    searchBook = false;
                }
            }
            System.out.println("[!] No book found with the given ISBN.");
        }```
Cause:
The code doesn't compile:
cannot find symbol
  symbol:   variable searchBook
  location: class 
cannot find symbol
  symbol:   variable bookISBN
  location: class 
cannot find symbol
  symbol:   variable scan
  location: class 
cannot find symbol
  symbol:   variable bookISBN
  location: class 
cannot find symbol
  symbol:   variable bookISBN
  location: class 
cannot find symbol
  symbol:   class Book
  location: class 
cannot find symbol
  symbol:   class Book
  location: class 
cannot find symbol
  symbol:   variable bookISBN
  location: class 
cannot find symbol
  symbol:   variable books
  location: class 
cannot find symbol
  symbol:   class Book
  location: class 
cannot find symbol
  symbol:   variable bookISBN
  location: class 
cannot find symbol
  symbol:   class Book
  location: class 
cannot find symbol
  symbol:   variable bookISBN
  location: class 
cannot find symbol
  symbol:   variable books
  location: class 
cannot find symbol
  symbol:   variable searchBook
  location: class 

## System out
[Nothing]
#

<@&987246399047479336> please have a look, thanks.

blissful schooner
#

suchBook.getIsbn(suchBook) why do you need to pass the book to this method? It's already an instanced method with access to this, there should be no need for this.

#

also your .remove doesn't work because it compares by reference. You should use the index of the found book instead and pass that to remove.

tall idol
#

removeIf is what you need

#

returns a boolean if it did anything