Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HashMap

 
Gary Ba
Ranch Hand
Posts: 161
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
  • 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
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
here is the exact line. number is an int


thank you Janeice
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
YAY!!!

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic