How can I add elements in Hashmap?
<p>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:</p>

<p><strong>Player:</strong></p>

<pre><code>public class Player {

public Player() {
}

public void setScore(int score) {
    this.score = score;
}

public void setName(String name) {
    this.name = name;
}

private String name;
private int score;

public Player(String name, int score) {
    this.name = name;
    this.score = score;
}
 public String getName() { 
     return name;
 }

@Override
public String toString() {
    return "Player{" + "name=" + name + "score=" + score + '}';
}

public int getScore() {
    return score;
}
</code></pre>

<p><strong>main:</strong></p>

<pre><code>Scanner scanner = new Scanner(System.in);
HashMap&lt;Integer,Player&gt; name= new HashMap&lt;Integer,Player&gt;();

    System.out.printf("Give the number of the players ");
    int number_of_players = scanner.nextInt();


    for(int k=1;k&lt;=number_of_players;k++)
     {


         System.out.printf("Give the name of player %d: ",k);
         name_of_players= scanner.nextLine();
         name.put(k, new Player(name_of_players,0));//k=id and 0=score

     }


   //This for finally returns the score and          
    for(int k=1;k&lt;=number_of_players;k++)
  {  
      Player name1 = name.get(k);
     System.out.print("Name of player in this round:"+name1.getName());
    ..............
    .............
    int score=p.getScore();
    name.put(k,new Player(name1.getName(),scr));//I think here is the problem

    for(int n=1;n&lt;=number_of_players;n++)//prints all the players with their score
      {

     System.out.print("The player"+name1.getName()+" has "+name1.getScore()+"points");

      }
</code></pre>

<p>Does anyone know how can I finally print for example:</p>

<pre class="lang-none prettyprint-override"><code>"The player Nick has 10 points.
 The player Mary has 0 points."  
</code></pre>

<p><strong>Update:</strong></p>

<p>I did this in main(as Jigar Joshi suggest)</p>

<pre><code> name.put(k,new Player(name1.getName(),scr)); 
 Set&lt;Map.Entry&lt;Integer, Player&gt;&gt; set =   name.entrySet();

 for (Map.Entry&lt;Integer, Player&gt; me : set) 
 { 
 System.out.println("Score :"+me.getValue().getScore() +" Name:"+me.getValue().getName()); 

}
</code></pre>

<p>and it prints "Score :0 Name : a Score :4 Name : a" when i put two names of players "a" and "b".I think the problem is here </p>

<pre><code> name.put(k,new Player(name1.getName(),scr));
</code></pre>

<p>How can i put the names in "names_of_players" of my previous for?             </p>
