• 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 confusion

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm back again
I'm trying to use a HashMap to hold part numbers, a quantity and a description.
I'm getting an error when I compile my code, the saying"method put(java.lang.String, int) not found in class java.util.HashMap."
I'm thinking i'm getting this error because you cann't put an int into a HashMap. Is this thinking correct?
here's a bit of the code i'm using

more questions i want to check for a key number, in this case it's partNum and if the key is there display the partNum, quantity, and description. Would the above code put both quantity and description with the same key, partNum. Or do I need to create an object to combine both description and quantity. And then put that object in the h.put?

And if i have to create an object combining quantity and description how would i pull that information out seperatly inorder to display it.
Any help would be appreciated.
Thanks,
Chris
 
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. You are right, you can't put an int (or any primitive for that matter) in a HashMap or other Collection classes. You can use the wrapper class Integer instead.
2. When you put a new value for the same key, the new value replaces the old value. So you probably want to create an object which consists of quantity and description and put it for this key.
3. In order to store and retrieve that info quckly, at simpest level, you can mark these two variables of the object (quantity and description) *public*, for a more OOPs compliant design, have get and set methods for these two variables.
HTH,
- Manish
 
chris czinder
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the information,
I'm currently trying to implement an object to contain both quantity and description. Though i'm having a bit of a problem w/that.
I've got a outter class App and an inner class Part. Part is where I'm putting the object and some get and set methods.
Here is the code. (simplified a bit)

This is not the complete code for the program, but I think enough to show the layout, and hopefully why App cannot use the Part(int, String) which is setup in the Part inner class.
Again any help, hints or suggestions would be greatly appreciated.
Thanks
Chris
 
Manish Hatwalne
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use this -

BTW, why do u think that inner class should be static?
HTH,
- Manish
 
chris czinder
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, changing App.Part.Part()
to new App.Part() worked.
As for why I chose static, it's because I kept getting an error stating,"Non-Static cannot be used in a Static" ... it was something like that I cannot remember the exact wording. I figured well if it wants static I'll give it static.
It seems to work now, was I wrong to do that?
Thanks again for the help, now I'm off to make this thing find and display info from the HashMap. Wish me luck
Thanks,
Chris
reply
    Bookmark Topic Watch Topic
  • New Topic