• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

How can I add elements in Hashmap?

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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."
 
Sheriff
Posts: 28322
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 28322
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It was just a tip, to turn your code more readable.

 
Ranch Hand
Posts: 58
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here goes my solution.

 
Sebanti Sanyal
Ranch Hand
Posts: 58
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If player name is unique, the same can be used as the key,and solution becomes much simpler.

 
nick dimi
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 58
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Marshal
Posts: 79952
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
I was born with webbed fish toes. This tiny ad is my only friend:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic