Results 1 to 6 of 6
  1. #1
    sony1 is offline Member
    Join Date
    Aug 2010
    Posts
    6
    Rep Power
    0

    Default What to modiy to get the output as HelloWorld

    Why is the ouput not coming as Hello World?
    What is to be modified?

    import java.util.*;

    class Key {
    int m_keyVal;

    Key( int val) {
    m_keyVal = val;
    }
    }

    public class TestKey {
    static HashMap<Key,String> table = new HashMap<Key,String>();

    static public void addToMap( int val, String str) {
    Key key = new Key( val);
    table.put( key, str);
    System.out.println(key);
    System.out.println(str);
    }

    static public String getString( int val) {
    Key key = new Key( val);
    System.out.println(table.get(key));
    return table.get( key);
    }

    public static void main(String[] args) {
    addToMap( 1, "hello");
    addToMap( 2, "world");

    System.out.println( getString( 1) + ", " + getString( 2) );
    }
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,274
    Rep Power
    25

    Default

    What do you get as output with the program you posted?

  3. #3
    sony1 is offline Member
    Join Date
    Aug 2010
    Posts
    6
    Rep Power
    0

    Default

    its all null

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,274
    Rep Power
    25

    Default

    You need to provide an equals and hashcode method to the Key class.
    Read the API doc for the get method. And Object class's equals method.

  5. #5
    sony1 is offline Member
    Join Date
    Aug 2010
    Posts
    6
    Rep Power
    0

    Default

    Can u tell me how to do that?I am not able to get u.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,274
    Rep Power
    25

    Default

    Do you know how to add methods to classes?
    If not, back to the books.

    Read the API doc for the Object class to get the signature for the equals and hashcode methods.
    The HashMap class uses these methods to work with keys.
    Add those two methods to your Key class.
    For debugging, add println() statements to both of those classes to see when they are called and what the args are that are being passed to them.

    For the guts of equals, its up to you to determine when two objects are equal.
    For the hashcode, I'm not sure what needs to be there. What does the API doc in the Object class suggest?

Similar Threads

  1. How to Run HelloWorld program?
    By siji44 in forum New To Java
    Replies: 8
    Last Post: 03-24-2010, 08:09 AM
  2. HelloWorld in EJB!
    By bdtagar in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 01-04-2010, 11:55 AM
  3. Struts2 helloworld application.
    By makpandian in forum New To Java
    Replies: 0
    Last Post: 06-02-2009, 11:19 AM
  4. About Java HelloWorld
    By Dankydoo in forum New To Java
    Replies: 0
    Last Post: 01-11-2009, 10:08 PM
  5. Run a helloWorld in NetBeans
    By mathias in forum NetBeans
    Replies: 1
    Last Post: 08-07-2007, 01:16 AM

Posting Permissions

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