Using HashMaps, can't get 'put' method to work.

I get the error 'Cannot resolve symbol' (the symbol being the '.' after the name of the HashMap) when using the put method when trying to add entries to a HashMap. I'm using Java 2 SDK1.4 testing my scripts currently on Windows.

I've tried finding out about this error using web-searches and various Java books but I guess it must be something to do with the Java setup I'm using. Any help would be most appreciated.

Here is the code

import java.util.HashMap;

public class HashDemoGeneric {
  public static void main(String[] args) {
    HashMap map = new HashMap();

    map.put(1, "Ian");
    map.put(42, "Scott");
    map.put(123, "Somebody else");

    String name = map.get(42);
    System.out.println(name);
  }
}
patelajkAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zzynxSr. Software engineerCommented:
Replace:

>>    map.put(1, "Ian");
>>    map.put(42, "Scott");
>>    map.put(123, "Somebody else");

by

    map.put(new Integer(1), "Ian");
    map.put(new Integer(42), "Scott");
    map.put(new Integer(123), "Somebody else");

Keys and values should be objects


zzynxSr. Software engineerCommented:
and of course:

>> String name = map.get(42);

should be

      String name = map.get(new Integer(42));
zzynxSr. Software engineerCommented:
Another correction:

Because get() returns just an Object, you should cast the result to a String:

String name = (String) map.get(new Integer(42));
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

zzynxSr. Software engineerCommented:
So to be clear:
This should work:


import java.util.HashMap;

public class HashDemoGeneric {

  public static void main(String[] args) {
    HashMap map = new HashMap();

    map.put(new Integer(1), "Ian");
    map.put(new Integer(42), "Scott");
    map.put(new Integer(123), "Somebody else");

    String name = (String) map.get(new Integer(42));
    System.out.println(name);
  }
}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
If you update to the latest version of the JDK, you can use your code as is
CEHJCommented:
>>you can use your code as is

Well, not *quite* as is, but if you upgrade, let me know
CEHJCommented:
The following prints 'Scott'


import java.util.HashMap;

public class HashDemoGeneric {
      public static void main(String[] args) {
            HashMap<Integer, String> map = new HashMap<Integer, String>();

            map.put(1, "Ian");
            map.put(42, "Scott");
            map.put(123, "Somebody else");

            String name = map.get(42);
            System.out.println(name);
      }
}
zzynxSr. Software engineerCommented:
Thank you
patelajkAuthor Commented:
Thanks for the help with this one! IT support in this place is non-existent as they are really understaffed. Haven't managed to update to the latest JDK yet.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.