• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Working on trying to list number of Occurences in an array comprised of objects

 
Ranch Hand
Posts: 186
1
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greetings, I have an Array that I created of Object type flower (from a class that I had created). I am trying to gather the amount of the same objects within the array. To do so, I had to recreate the equals() method and re-write the toString() method. The elements appear correctly, but for some reason they are not being considered the same so for instance, this is the output of the program:

  • Welcome to my flower pack interface.
    Please select a number from the options below

    1: Add an item to the pack.
    2: Remove an item from the pack.
    3: Search for a flower.
    4: Display the flowers in the pack.
    0: Exit the flower pack interfact.
    1
    Please enter the name of a flower type to add:
    roses
    What is the color of the flower you would like to add?:
    red
    Please describe the scent of the flower in one line:
    magnificient
    Is the flower a thorny kind of flower? (Please answer yes or no with y or n only
    n
    1: Add an item to the pack.
    2: Remove an item from the pack.
    3: Search for a flower.
    4: Display the flowers in the pack.
    0: Exit the flower pack interfact.
    1
    Please enter the name of a flower type to add:
    roses
    What is the color of the flower you would like to add?:
    red
    Please describe the scent of the flower in one line:
    magnificient
    Is the flower a thorny kind of flower? (Please answer yes or no with y or n only
    n
    1: Add an item to the pack.
    2: Remove an item from the pack.
    3: Search for a flower.
    4: Display the flowers in the pack.
    0: Exit the flower pack interfact.
    1
    Please enter the name of a flower type to add:
    dandelions
    What is the color of the flower you would like to add?:
    yellow
    Please describe the scent of the flower in one line:
    neutral, no scent
    Is the flower a thorny kind of flower? (Please answer yes or no with y or n only
    n
    1: Add an item to the pack.
    2: Remove an item from the pack.
    3: Search for a flower.
    4: Display the flowers in the pack.
    0: Exit the flower pack interfact.
    4
    {null=22, yellow dandelions smells neutral, no scent is Thorny false
    =1, red roses smells magnificient is Thorny false
    =1, red roses smells magnificient is Thorny false
    =1}
    1: Add an item to the pack.
    2: Remove an item from the pack.
    3: Search for a flower.
    4: Display the flowers in the pack.
    0: Exit the flower pack interfact.


  • Now in regards to the code, to display the flowers, I'm using a HashMap data structure which incorporates both a key and value. The objective of the method display flowers is to count the number of same flowers and properly display them if they are the same

    For instance:

    Red Roses smells great isThorny false - 1
    White Roses smells magnificient isThorny false - 2

    etc. etc.

    The code is as follows:



    Here I override the original .equals Object method and create a toString method. However it is not being displayed properly. Is there anything wrong with my display method?



    Thanks!
     
    Marshal
    Posts: 79249
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Good grief! There is nothing like clear understandable use of the ?: operator and that is nothing like clear understandable use of the ?: operator
    Get rid of some of the return false statements and you can get rid of all the double negatives.
    So you need to iterate the flower array and find which equal a certain flower object?
     
    Sheriff
    Posts: 7125
    184
    Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Instead of the ternary operator (?:) in the if statement, I'd write:

    There is a place for brevity, and this is not the place!
     
    Ranch Hand
    Posts: 86
    18
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You most certainly do not override the hashCode() method, thus hash-based collections (e.g. HashMap) cannot operate correctly on your Flower-Type. ("Note that it is generally necessary to override the hashCode method whenever this [equals] method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.")

    Regarding the equals-check with possible null-values:
    is in my opinion preferable.
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic