• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Delete method not deleting hard coded books

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The delete function in my library program doesn't work properly, it won't delete the hard coded in the list but if I enter them in via user input it does, why is this?









Any help appreciated.
 
Marshal
Posts: 26456
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you referring to the inputBookToDelete() method, which creates a new (presumably empty) library and then tries to delete books from it?
 
Paul Clapham
Marshal
Posts: 26456
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


That's going to lead to confusion too. Why does the Library class have a static book list? Did you intend for all libraries to to have the same collection of books? (That isn't how libraries work where I live.)
 
Saloon Keeper
Posts: 7999
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your problem is that you are creating new libraries all over the place. In your entire project you should only be creating one new Library to represent the Cork Library. You have also made most of your fields public which is allowing inputs of new books a back door into a library from which delete succeeds.
 
Kevin O'Sullivan
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Are you referring to the inputBookToDelete() method, which creates a new (presumably empty) library and then tries to delete books from it?



Yup well that method takes the input and then the method in Library class deleteBooks() is suppose to take care of the deleting .
 
Paul Clapham
Marshal
Posts: 26456
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kevin O'Sullivan wrote:

Paul Clapham wrote:Are you referring to the inputBookToDelete() method, which creates a new (presumably empty) library and then tries to delete books from it?



Yup well that method takes the input and then the method in Library class deleteBooks() is suppose to take care of the deleting .



Like Carey said. Just create one Library. And it should have only one list of books (i.e. making that a static variable is wrong).
 
Kevin O'Sullivan
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Your problem is that you are creating new libraries all over the place. In your entire project you should only be creating one new Library to represent the Cork Library. You have also made most of your fields public which is allowing inputs of new books a back door into a library from which delete succeeds.



ok  I made one instance of library class but had to declare it I've a feeling this isn't good practice lol.
 
Kevin O'Sullivan
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Kevin O'Sullivan wrote:

Paul Clapham wrote:Are you referring to the inputBookToDelete() method, which creates a new (presumably empty) library and then tries to delete books from it?



Yup well that method takes the input and then the method in Library class deleteBooks() is suppose to take care of the deleting .



Like Carey said. Just create one Library. And it should have only one list of books (i.e. making that a static variable is wrong).




yup did this as well but still have the same problem, not deleting any hardcoded books.
 
Carey Brown
Saloon Keeper
Posts: 7999
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are declaring the entire world static. And no, that's not good practice.
This is your problem:
It forces you to use static just to shutup the compiler from complaining. You need to start off by creating an instance of your class and then using that.

 
Carey Brown
Saloon Keeper
Posts: 7999
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kevin O'Sullivan wrote:yup did this as well but still have the same problem, not deleting any hardcoded books.

Kevin, when you make a change and then say "nope, it doesn't work" we can't help you unless you show us all the code again.
 
Carey Brown
Saloon Keeper
Posts: 7999
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:

BTW this will force you to UNDO (almost?) all of those statics.
 
Kevin O'Sullivan
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:

Kevin O'Sullivan wrote:yup did this as well but still have the same problem, not deleting any hardcoded books.

Kevin, when you make a change and then say "nope, it doesn't work" we can't help you unless you show us all the code again.



Sorry about that carey, I'll give that a go and report back.
 
Marshal
Posts: 72406
315
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, where is your Book#equals() method? You won't be able to delete correctly without it. Remember to ensure all eight requirements are fulfilled:-
  • 1: You must override equals(); don't overload it.
  • 2: Also override hashCode()
  • 3: Reflexivity
  • 4: Symmetry
  • 5: Transitivity
  • 6: Consistency
  • 7: Consistency doesn't allow equals() to throw any exceptions
  • 8: It must return false if null is passed.
  • I know you can only see five •s here, but they come out to eight requirements.
     
    Kevin O'Sullivan
    Ranch Hand
    Posts: 92
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I've been reading up and watching videos on hashcode and equals method as I know nothing about them and I am having trouble understanding how to implement them, am I right in saying these methods should be in the book class ?
     
    Master Rancher
    Posts: 4250
    38
     
    Kevin O'Sullivan
    Ranch Hand
    Posts: 92
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Norm Radder wrote:Also posted here: https://www.javaprogrammingforums.com/whats-wrong-my-code/43659-delete-method-not-deleting-hard-coded-books.html

    Please read:   https://coderanch.com/wiki/660346/Wiki/Forthright-Cross-Posting-Sites




    Sorry norm wasn't aware of that.
     
    Campbell Ritchie
    Marshal
    Posts: 72406
    315
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Where did you read about hashCode() and equals()? What did they say? I worry about randomly‑found online tutorials; most of them are poor quality.
    I recommend you find Effective Java by Joshua Bloch (old edition here),  Odersky Sppon and Venners, and search for Angelika Langer Java equals() hashCode(). Make sure to get the English edition of Langer.

    Kindly don't delete your questions, neither here nor on the other forum.
     
    Norm Radder
    Master Rancher
    Posts: 4250
    38
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Where is the requirement for an equals method?  The specs say to delete a book with a given name.  
     
    Kevin O'Sullivan
    Ranch Hand
    Posts: 92
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:Where did you read about hashCode() and equals()? What did they say? I worry about randomly‑found online tutorials; most of them are poor quality.
    I recommend you find Effective Java by Joshua Bloch (old edition here),  Odersky Sppon and Venners, and search for Angelika Langer Java equals() hashCode(). Make sure to get the English edition of Langer.

    Kindly don't delete your questions, neither here nor on the other forum.




    Here

    Just they are used to compare objects and return true or false but I don't have any clue how to implement it for what I need to do.

    Tutorial

    and this video


     
    Carey Brown
    Saloon Keeper
    Posts: 7999
    70
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I'm with Norm here. Where did we figure we needed equals() and hashCode(). Using the String methods for the name field should suffice.
     
    Campbell Ritchie
    Marshal
    Posts: 72406
    315
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The video is worthless to beginners. He shows correct implementations of both methods without explaining why you need a particular implementation of equals(). Actually, it isn't quite correct because it can't cope correctly with any of the fields being null. It doesn't show what can go wrong if you don't implement equals() correctly.
    He also has some incorrect names for things. There are no “no‑op”s in that code. The lines he calls “no‑op”s actually cause something to be executed and fail to add something to the set; they also return something which we usually ignore. He calls things functions which aren't functions. He also doesn't explain why he is using a nested class, which is a very strange design, and he called the Person class immutable when it isn't strictly immutable. That is O too common a problem: you go through Google, you find something matching your requirements, and you don't have the experience to recognise its shortcomings.
    The tutorial link you gave us is slightly better, but it doesn't explain the rules behind equals(). So still of no value to you.
    Look in the three resources I gave you yesterday; they do explain the rules and why you need a particular implementation. It is worth reading all three because they all take a slightly different approach. You can only understand the implementation details when you understand the rules behind them. Both your links described hash codes a lot better, which is hardly surprising. Hash codes are much easier to understand.
    But there are two more points:-
  • 1: The “alphabet of variables” used to calculate the hash code must not contain anything not used to determine equality.
  • 2: When you write an equals() method, you are determining your definition of equality and it is your definition of equality that will be used for ever. Don't change that definition as the codejava link did, otherwise you can break other code. You do have the option to define two books as equal if they have the same title, but you would be stuck with that definition of equality forever.
  • What codejava ought to have written, and what is similar to what I would have told you to write if I had actually read your requirements to look only at the title (sorry), is this:-It won't work until you corrrect my spellling misteak. There are also methods in the Collection interface to remove all elements matching a particular condition.
     
    Kevin O'Sullivan
    Ranch Hand
    Posts: 92
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks a lot for your help, I ordered the effective java book, can't wait to give it a read, seems to be a popular book.
     
    Kevin O'Sullivan
    Ranch Hand
    Posts: 92
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Haven't had a chance to try the above yet but Thanks everyone for all yer help, I ordered the effective java book, can't wait to give it a read, seems to be a popular book.
     
    This is my favorite show. And this is my favorite tiny ad:
    SKIP - a book about connecting industrious people with elderly land owners
    https://coderanch.com/t/skip-book
    reply
      Bookmark Topic Watch Topic
    • New Topic