Results 1 to 9 of 9
  1. #1
    trueblue is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default TreeMap class and message not understood issues - help please

    Can anyone help with this one please?
    The code is the start of a Map interface/TreeMap class to hold names as keys and addresses as values (both Strings).
    I think the instance variable and the constructor are correct, but the addAddress method throws an error
    Semantic error: Message addAddress( java.lang.String, java.lang.String ) not understood by class'java.util.TreeMap'.
    I can only guess that I've overlooked something quite simple? The line at the end of the code should read
    "Replaced "old address" with "new address" for "new name" and this should only display if the key already exists in the Map,
    hence the if statement. The keys and values have been declared as Objects for future flexibility. Sorry if this is all blatantly obvious.
    Also if I wanted to test this code and create an instance of the Addressbook class, what should my code look like?
    Many thanks.

    Java Code:
    import java.util.*;
     
    public class Addressbook
    {
       
       private static Map<Object, Object> addresses; 
     
     
       public Addressbook()
       {
          super();
          this.addresses = new TreeMap<Object, Object>();
       }
     
       public static void addAddress(String name, String address)
       {
          if (addresses.containsKey(name))
          {
             addresses.put(name, address);
             System.out.println("Replacing " + addresses.remove(name) + " with " 
                                             + addresses.get(name) + " for " + name);
          }  
          else
          {
             addresses.put(name, address);
          }
       }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by trueblue View Post
    Can anyone help with this one please?
    The code is the start of a Map interface/TreeMap class to hold names as keys and addresses as values (both Strings).
    I think the instance variable and the constructor are correct, ...
    Constructors are meaningless in the face of static methods and fields. Please do yourself a favor and get rid of all statics in this class.

    Also, this makes little sense:
    Java Code:
             addresses.put(name, address);
             System.out.println("Replacing " + addresses.remove(name) + " with " 
                                             + addresses.get(name) + " for " + name);
    as you've already replaced the address for name by the time you call the SOP.

  3. #3
    trueblue is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Constructors are meaningless in the face of static methods and fields. Please do yourself a favor and get rid of all statics in this class.

    Also, this makes little sense:
    Java Code:
             addresses.put(name, address);
             System.out.println("Replacing " + addresses.remove(name) + " with " 
                                             + addresses.get(name) + " for " + name);
    as you've already replaced the address for name by the time you call the SOP.
    OK I can remove the statics.
    Also I think I was clutching at straws using
    Java Code:
    addresses.remove(name);
    because this returns the original value for the key which is what needs to be displayed at this point. What would you suggest?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    If have the least amount of respect for us and any help you may get here and elsewhere, please do not cross-post the same question in multiple fora without being up-front about it. Repeated infractions may decrease members' willingness to help you in the future.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by trueblue View Post
    Java Code:
    addresses.remove(name);
    because this returns the original value for the key which is what needs to be displayed at this point.
    After you've changed it??

  6. #6
    trueblue is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    After you've changed it??
    Well I changed the order within the if statement expecting it might help, but it didn't :(

    Sorry about the incorrect format of my posts. No disrespect intended whatsoever.

    Java Code:
    public void addAddress(String name, String address)
       {
          if (addresses.containsKey(name))
          {
             
             System.out.println("Replacing " + addresses.remove(name) + " with " + addresses.get(name) + " for " + name);
             addresses.put(name, address); 
          }  
          else
          {
             addresses.put(name, address);
          }
       }
    }

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Of course your changes above are not going to work. You have to think like a computer and walk through your code. For instance, in your first bit of code you do this:

    1. Change the address value for the name key, and then
    2. Remove the address for the name key, and extract the value added in step 1 (and removing it at the same time), and then
    3. Attempt to show a value from a key that's been removed already.

    Do you see how this won't work?

    Now in your most recent post, you
    1. Remove the value from the map for the key name.
    2. Attempt to get a "new" value from the map for the removed key, before adding anything new to the map, and
    3. Then you add a new value for the key name.
    Again, do you see why this won't work?

    Perhaps you want to remove the value and store it in a variable, add a new value and store the address in a variable, and then use the variables to show the output. Again, you must think through the problem before writing the code, else the code won't make any sense.

    Sorry about the incorrect format of my posts. No disrespect intended whatsoever.
    It has nothing to do with "format" here. Can you imagine the frustration of anyone who tries to help you only to find out later that the same answer was given hours ago in a cross-posted thread? No one likes wasting their time, especially a volunteer. The polite thing to do would be to not do this, but if you feel that you absolutely must, to at least provide links in both cross-posts to each other.

  8. #8
    corlettk is offline Member
    Join Date
    Apr 2009
    Location
    Brisbane
    Posts
    86
    Rep Power
    0

    Default

    Maybe something like
    Java Code:
      public void add(String name, String address) {
        if (addresses.containsKey(name)) {
          System.err.println("DEBUG: Replacing \""+addresses.remove(name)+"\" with \""+address+"\" for \""+name+"\"");
        }
        addresses.put(name, address);
      }
    As a general rule of thumb do NOT mix "output" with "process"... ergo: It's a very bad idea to output anything from an "add" method... DEBUG-prints are OK, because they're not part of "the product"... they will all be disabled (or removed) before you ship your program.

  9. #9
    trueblue is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Of course your changes above are not going to work. You have to think like a computer and walk through your code. For instance, in your first bit of code you do this:

    1. Change the address value for the name key, and then
    2. Remove the address for the name key, and extract the value added in step 1 (and removing it at the same time), and then
    3. Attempt to show a value from a key that's been removed already.

    Do you see how this won't work?

    Now in your most recent post, you
    1. Remove the value from the map for the key name.
    2. Attempt to get a "new" value from the map for the removed key, before adding anything new to the map, and
    3. Then you add a new value for the key name.
    Again, do you see why this won't work?

    Perhaps you want to remove the value and store it in a variable, add a new value and store the address in a variable, and then use the variables to show the output. Again, you must think through the problem before writing the code, else the code won't make any sense.


    I am still frigging about with this address book.
    I now need to create a method which takes an address (a value) as an argument and returns an address/value if the address exists in the addressbook and is removed along with the name/key. It then displays the address/value it has removed, or "Address not found" if the address did not exist. I have done something similar when using a name/key as an argument, but it seems trickier when taking an address/value as an argument.
    Can anyone offer any guidance on this please? Here is my incomplete code which currently just caters for addresses/values which do not exist. Many thanks.

    Java Code:
    public Object forgetFromAddress(Object address)
       {
          Object addressRemove = "";
          if (!addresses.containsValue(address))
          {
             addressRemove = "Address not found";
          }
          return addressRemove;

Similar Threads

  1. Issues with posting
    By xcallmejudasx in forum Forum Lobby
    Replies: 1
    Last Post: 06-27-2009, 03:27 AM
  2. Sorting Elements in a TreeMap
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-12-2008, 09:47 PM
  3. How to use treemap
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-12-2008, 09:47 PM
  4. Issues with Jva I.O
    By Annatar01 in forum New To Java
    Replies: 0
    Last Post: 02-08-2008, 02:16 AM
  5. Issues with a while loop
    By Lang in forum New To Java
    Replies: 4
    Last Post: 11-09-2007, 10:01 PM

Posting Permissions

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