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; }