• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Naziru Gelajo
Ranch Hand
Posts: 175
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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!
     
    Campbell Ritchie
    Marshal
    Posts: 56533
    172
    • Mark post as helpful
    • send pies
    • 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?
     
    Knute Snortum
    Sheriff
    Posts: 4276
    127
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Mark post as helpful
    • send pies
    • 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!
     
    Tobias Bachert
    Ranch Hand
    Posts: 86
    18
    • Mark post as helpful
    • send pies
    • 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.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!