• Post Reply Bookmark Topic Watch Topic
  • New Topic

HashMap get value by key  RSS feed

 
Tai Yo
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Guys!
Maybe my question seems so simple, but I couldn't solve it.

I have a HashMap in Java like this (I w'd like to put values from element table):

Different keys have same value.
But as you know in table more than 100 elements also including their nuclides they become around 3000.
For example, when user type "Al27" (2nd HashMap) it outputs ""13-AL-27.
But in case if user type "Mg24" or other elements how can I initialize variables x, y, z, so they could work for all elements?
I tried to do like this:

But it doesn't work.
Any help is really appreciated.


 
Shaun Wild
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may want to consider rewording this thread or being more specific, I really can't make out what you're actually trying to achieve.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tai Yo wrote:Different keys have same value....

It sounds like that's part of the problem, because a Java Map can only contain distinct keys.

Any attempt to put() a new mapping with a key that is already in the Map will result in its value being replaced, and the method will return the "old" value.

However, you also have another problem: x and y are ints, so x+y (or y+x) is ALSO an int; but in line 5 you are trying to put() it into a Map that is expecting a String as a key, so you are presumably getting an exception.

Winston
 
Carey Brown
Bartender
Posts: 3022
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you get stuck on a problem like this it helps to add some print statements to see what the program is actually doing.

The output from this is:

Is this not what you expected? How is it different?

Possibly on the key side you could have code that strips out special characters. That might simplify things.
 
Tai Yo
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for replies.
I solved the challenge another way by extracting numbers and alphabets from jTextField.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!