I need to find in a ArrayList<Libro>
through Libro
a passed parameter(isbnreceived). the parameter it receives(isbn) is a type attribute of int
the Book object, but I can't find the formula inside the for
to find the book. The functionality also asks that if it finds the book it saves it in:
answerbook
This is my code:
for (int i = 0; i < coleccion1.size(); i++) {
if(isbnrecibido == coleccion1(i).getisbn()) {
librorespuesta = coleccion1.get(i);
System.out.println(coleccion1.get(i));
}
}
Suppose your class structure
Libro
isWe create a class
ProbarLibro
to create the books and perform the search for a book.What you are doing is almost correct, generally we search for an object based on the value of some property in this case based on the ISBN.
But remember that if this value is of type String, the comparison must be done using the
.equals(..)
or function.equalsIgnoreCase(..)
that compares the text string against an object ignoring case. :One way you could make it easier to find your books is through Streams.
As a recommendation, I will tell you that there are books with ISBN 13, so a type data
int
would be insufficient, so I recommend changing the ISBN to String or long.Let's imagine that your book class is as follows:
We could find the book by its ISBN with Stream.filter and since the ISBN is unique we can limit it to the first result with Stream.limit
Output
I did not understand your question very well but I think the solution to your problem is something like this
As I told you in the comment, be careful with ==, it is used to compare addresses and primitive types. The code I told you is an example of what I think happens to you.