Help coderanch get a
new server
by contributing to the fundraiser
  • 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

HashMap question

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The preceding code gives me compile error. I tried to find out why but could not figure out. Any ideas ?
-Sam
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sam, your print statement references a method which is undefined in your class (containsValue()). You should associate the method with the HashMap object you created.
 
sam pitt
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OOPS!! (No pun intended)
Thanks..

[This message has been edited by sam pitt (edited July 13, 2001).]
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi im the same program above if i use constainsKey(),
pls c below prgm it gives me false :even though the key exists
why?
import java.util.*;
public class MyClass
{
public static void main (String args[])
{
HashMap hm = new HashMap();
hm.put(new Integer("1"),"Tic tac toe");
hm.put(new Integer("3"),"Checkers");
hm.put(new Integer("2"),"Hockey");
hm.put(new Integer("4"),"Chess");
System.out.println(hm.containsKey("1"));
}}
 
author and deputy
Posts: 3150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the last code will always prints false..
u have to use hm.containsValue("Chess");
please correct me if i'm wrong..
the correct code is
<code>
import java.util.*;
public class MyClass
{
public static void main (String args[])
{
HashMap hm = new HashMap();
hm.put(new Integer("1"),"Tic tac toe");
hm.put(new Integer("3"),"Checkers");
hm.put(new Integer("2"),"Hockey");
hm.put(new Integer("4"),"Chess");
System.out.println(hm.containsKey(new Integer("1")));
System.out.println(hm.containsValue("Chess"));
}}
</code>
thanks
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic