• 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

 
Ranch Hand
Posts: 161
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,
I am working on OOP-2 and running into hashmap issue. Here is what I have (in words).

I created a HashMap<String, int>, initialized it to the one I wanted but when I am getting (get()) it using the 'key', the compiler gives me an error which says:
incompatible types.
found: java.lang.Object
required: int

when assigning to an int.

Since I put int on my hashmap, should I not get an int back?
Also, I tried to assign it to Object and compiled okay. The only thing is I need a number in order for me make some multiplication.



Anyone guys,
Garry
 
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So your key is the String and the value is the int?

WAIT.... try wrapping it -- use Integer in your generics declaration instead of int.
 
Gary Ba
Ranch Hand
Posts: 161
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It did not work. I tried it on my code and with a simple main test, both did not work. I get the same compile error.
 
Janeice DelVecchio
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So the key is a String and the value is an Integer?

So when you want to access something by key, you would:



Is that the way you're using it?
 
Gary Ba
Ranch Hand
Posts: 161
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here is the exact line. number is an int


thank you Janeice
 
Janeice DelVecchio
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So if the numberString[] array is a String array (and [0] is actually populated with a String), and the key of the HashMap is a String.... and 'number' is an Integer..... and the Map was declared as <String, Integer>.....

it seems you're doing everything right.

I just want to remind you that, well I'm 95% sure, HashMap and other Collections like it (TreeMap, ArrayList) don't hold primitives... they hold wrapper/boxed classes. That could be the issue why you're getting an "Object" and it expects an "int". You may try casting the int 'number' as an Integer, but autoboxing should take care of that. **shrug**

JD

P.S. If your HashMap is really named hashMap, it's not good naming convention. I dunno if the Nitpickers nitpick that, but I will.
 
Gary Ba
Ranch Hand
Posts: 161
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
do'h.

I found what was wrong with it from your comment. I forget to specify new HashMap<String, Integer>().


Garry
 
Janeice DelVecchio
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
YAY!!!

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic