Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Nov 2017
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Prevent adding double names in Arraylist

    I have this Arraylist:

    Code:
    private ArrayList<Book> library;
    
    //add method
    
    public void newBook(Book book)
    {
    
    library.add(book);
    
    if(book == library.getName) {
    
      System.out.println("The book already exists."); 
     }
    } 
    else {
       System.out.println("You added a new book.");
    What would the syntax be to only add books with unique names and not dublicates. My if statement is probobly wrong.

  2. #2
    New Coder
    Join Date
    Jan 2016
    Location
    Malaysia
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You should use .equals() to compare String


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •