aspose file tools*
The moose likes Beginning Java and the fly likes How can I add elements in Hashmap? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How can I add elements in Hashmap?" Watch "How can I add elements in Hashmap?" New topic
Author

How can I add elements in Hashmap?

nick dimi
Greenhorn

Joined: Dec 21, 2011
Posts: 13
I make a java game. At first the program asks for the number of the players, after that for their names. I put their names in a Hashmap with an id and their score. At the end of the game I count the score and I want to put it in the Hashmap (the specific score for the specific name). Does anyone know how to do this? This is my code:
Player


main:
Does anyone know how can I finally print for example:

"The player Nick has 10 points.
The player Mary has 0 points."
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18564
    
    8

I'm not sure I understand your requirement. It sounds like you want the key of the map to be the player's name, and the value to be the player's score. Is that correct?
nick dimi
Greenhorn

Joined: Dec 21, 2011
Posts: 13
Paul Clapham wrote:I'm not sure I understand your requirement. It sounds like you want the key of the map to be the player's name, and the value to be the player's score. Is that correct?

The key is an id(i did it because i thought it would be useful if i wanted to search a player with its id) and in Player i put the name and the score.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18564
    
    8

Okay. So I can tell your requirement is to use a Hashmap, but from there on I don't understand. Perhaps if you explain your requirement clearly it will help you to write code implementing that requirement.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
You put a Player instance in the map at the start of the game. When you are printing the scores, I don't understand why you instantiate a new Player object and put that in the map instead. This looks highly suspicious to me. I would expect a single Player instance for each player to last for the duration of the game. I would expect that, whenever there is new information about a player -- for example, a change in the score -- that the code would go to the map, get the existing Player object and update it.

As Paul said, it would help if you explain to us more specifically what you are doing.
nick dimi
Greenhorn

Joined: Dec 21, 2011
Posts: 13
Dennis Deems wrote:You put a Player instance in the map at the start of the game. When you are printing the scores, I don't understand why you instantiate a new Player object and put that in the map instead. This looks highly suspicious to me. I would expect a single Player instance for each player to last for the duration of the game. I would expect that, whenever there is new information about a player -- for example, a change in the score -- that the code would go to the map, get the existing Player object and update it.

As Paul said, it would help if you explain to us more specifically what you are doing.


Thanks for your answers!I want to do what you just described at your answer...i thought that the right way to do this it is to put a new player but as you said this is wrong...because i want to keep information about each player...so i have to change the "new Player" in hashmap?
Raphael Onofre
Greenhorn

Joined: Dec 29, 2011
Posts: 8
I advise you to see the keySet method to list the elements on the HashMap. I think, your code will be much more clear.

E.g.:

nick dimi
Greenhorn

Joined: Dec 21, 2011
Posts: 13
Raphael Onofre wrote:I advise you to see the keySet method to list the elements on the HashMap. I think, your code will be much more clear.

E.g.:





i dont understand how to use it in my case. . . :/
Raphael Onofre
Greenhorn

Joined: Dec 29, 2011
Posts: 8
It was just a tip, to turn your code more readable.

Sebanti Sanyal
Ranch Hand

Joined: Nov 07, 2011
Posts: 58

Here goes my solution.

Sebanti Sanyal
Ranch Hand

Joined: Nov 07, 2011
Posts: 58

If player name is unique, the same can be used as the key,and solution becomes much simpler.

nick dimi
Greenhorn

Joined: Dec 21, 2011
Posts: 13
Sebanti Sanyal wrote:If player name is unique, the same can be used as the key,and solution becomes much simpler.



Hello and thanks for your answer!I had a problem with my access to internet and now i see your post.
nick dimi
Greenhorn

Joined: Dec 21, 2011
Posts: 13
Well,these days i wrote some more code for my program and the last for look like this :
but now in do-while i cant go to the next player...Can you help me one more time please?
Sebanti Sanyal
Ranch Hand

Joined: Nov 07, 2011
Posts: 58

nick dimi wrote:Well,these days i wrote some more code for my program and the last for look like this :
but now in do-while i cant go to the next player...Can you help me one more time please?

I don't really understand the purpose of the do-while loop. 'for(String name:players.keySet())' will itself take you to the next player.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38765
    
  23
You have misidentified those loops. The outer loop is a for-each loop (officially called enhanced for loop) and inside that you have a do loop which appears to go on for ever Have a look at the link I gave you; you can see that the for-each loop will look at every element in that set, but not in any predictable order.

Please check your indentation; correct indentation makes the code much easier to understand.
nick dimi
Greenhorn

Joined: Dec 21, 2011
Posts: 13
Sebanti Sanyal wrote:
nick dimi wrote:Well,these days i wrote some more code for my program and the last for look like this :
but now in do-while i cant go to the next player...Can you help me one more time please?

I don't really understand the purpose of the do-while loop. 'for(String name:players.keySet())' will itself take you to the next player.



Thanks a lot for your help.I made some changes and i dont use do-while anymore.Do you know how can i get the player with the highest score?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7775
    
  21

nick dimi wrote:Do you know how can i get the player with the highest score?

Well, you could go through all the Players; but the quickest method may be to cache the value whenever you call
player.setScore(player.getScore()+10);

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can I add elements in Hashmap?