Equals method override and ArrayList | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Equals method override and ArrayList

Hi, I am using generated code (equals method) to see if user entered values (book title and publication year) are or are not already a part of an ArrayList of type 'Book'. public static void main(String[] args) { Scanner scanner = new Scanner(System.in); ArrayList<Book> books = new ArrayList<>(); while (true) { System.out.println("Name (empty will stop):"); String name = scanner.nextLine(); if (name.isEmpty()) { break; } System.out.println("Publication year:"); int publicationYear = Integer.valueOf(scanner.nextLine()); Book addBook = new Book(name, publicationYear); for (int j = 0; j < books.size(); j++) { if (addBook.equals(books.get(j))) { System.out.println("SAME BOOK"); } else { System.out.println("COOL NOT THE SAME"); books.add(addBook); } } // NB! Don't alter the line below! System.out.println(books); System.out.println("Thank you! Books added: " + books.size()); } } } *** Where I am having trouble is the IF statement. If I run the program it keeps returning the wrong result. The 'equals' method is below. I even used the auto-generate feature so I know it's right.... I'm pretty sure my problem is in the Main method... PLEASE HELP! Been struggling with this for about 2 weeks!! Thanks @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final Book other = (Book) obj; if (this.publicationYear != other.publicationYear) { return false; }

25th Feb 2021, 1:33 AM
Jon Barto
Jon Barto - avatar
8 Answers
+ 2
why not? if they fit in one file (or you generate file dynamically)... I wasn't asking for removing your working example/base, but for adding the link to a code playground with your code ;)
25th Feb 2021, 2:01 AM
visph
visph - avatar
+ 1
firstly you've forgotted to import ArrayList class (that's the error wich stop your code) second, your comparison loop (in main) have logical errors... you should set a flag if the book is already in the list, and outside the loop output message about found or not: https://code.sololearn.com/cY53DQdltmhg/?ref=app
25th Feb 2021, 2:49 AM
visph
visph - avatar
+ 1
try it yourself ;) I think you can remove the 'if (0<books.size())' I've put it before the for loop... (done before exiting if...else from it)... with the if...else inside, you're outputting something for each book already present (first insert output nothing (as size is zero), second output only once, third two.... and so on)
25th Feb 2021, 1:25 PM
visph
visph - avatar
0
it's return correct result for me... ... and I'm disapointed because good redacted questions, with relevant tags are not numerous here :(
25th Feb 2021, 1:40 AM
visph
visph - avatar
0
or maybe your code is not the same as the linked one? (I just noticed that you're not it's original author)... if so, share a playground with your complete code (actually the code typed in the description is truncated, and having a ready to run copy help)...
25th Feb 2021, 1:45 AM
visph
visph - avatar
0
I edited my original post to show just the code I am using. Can Code Playground have different classes?
25th Feb 2021, 1:59 AM
Jon Barto
Jon Barto - avatar
0
I put it in the Code Playground but it doesn't run. I am only familiar with IDEs that use multiple tabs for different classes. I am just beginning JAVA (well, have been self teaching for the past few months). Not to sound ungrateful but I don't even know how to properly word my question. I think I'm comparing a user defined object (also an ArrayList entry... to make matters worse) with an object of class Book (?) It's so confusing and frustrating I am at the end of my rope here. Here is the Code Playground link if you want to look at it there... https://code.sololearn.com/cA11A24A7a13/#java
25th Feb 2021, 2:11 AM
Jon Barto
Jon Barto - avatar
0
@visph, I tried your fix and it works - thank you. My question now is why couldn't the logic be a part of the IF statement? Is that just not possible? From looking at your 'match' logic it seems like it is the same as what I was doing before? Obviously it's different since your solution works... but why?
25th Feb 2021, 12:53 PM
Jon Barto
Jon Barto - avatar