title: java - why does my code fill up all values
body: <p>This is the method in my program which deals an 'initial hand' to players in the game, the players array is an array which contains Player objects. The Player objects themselves are arrays or the Card object, which are defined in other classes.      </p>
  
  <pre><code>private static void dealInitialHand(int noPlayers, Player[] players, int noCardsInHand ){
     System.out.println( "\nPlease enter the name of the deck file" );
     File theDeck = new File (Checks.userInputCheck( "deckfile.txt" ) );
     int intLine;
  
     try{
         //fileReader = new BufferedReader ( new FileReader( theDeck ) );
         Scanner fileScanner = new Scanner( theDeck );
  
         int line = 0;
  
         for( int h = 0; h &lt; noCardsInHand; h++){
             for( int p = 0; p &lt; noPlayers; p++){
                  System.out.println( "Player " + h + " Card" + p );
  
                  line = Integer.parseInt( fileScanner.nextLine() );
  
                  players[p].setHand( line, h );
  
             }
         }
         seePlayerHands();
     }catch (Exception e) {
         e.printStackTrace();
     }
  }
  </code></pre>
  
  <p>The problem I have is when I go to assign the card values to the certain card for the certain player it assigns that value to all the cards for all the players and i don understand why it does this.</p>
  
  <p>This is printed when iterating over the array, to show me that it is pointing to the correct place.</p>
  
  <pre><code> Player 0 Card0
   Player 0 Card1
   Player 0 Card2
   Player 1 Card0
   Player 1 Card1
   Player 1 Card2
   Player 2 Card0
   Player 2 Card1
   Player 2 Card2
  </code></pre>
  
  <p>But when the seePlayerHands() methods this is the result if the last card that was read is a 2 </p>
  
  <pre><code> Player 1's hand is currently
   2
   2
   2
   Player 2's hand is currently
   2
   2
   2
   Player 3's hand is currently
   2
   2
   2
  </code></pre>
  
  <p>instead of being this </p>
  
  <pre><code> Player 1's hand is currently
   4
   2
   5
   Player 2's hand is currently
   3
   2
   2
   Player 3's hand is currently
   5
   5
   2
  </code></pre>
  
  <p>from the file which looks like </p>
  
  <pre><code> 4
   3
   5
   2
   2
   5
   5
   2
   2
   4
   4
   6
   3
   5
   4
   6
   2
   3
  </code></pre>
  
  <p>help would be aprreciated</p>
  
  <p>just putting everything here</p>
  
  <pre><code>  public class CardGame{
    static Player[] players;
    static int handSize;
    public static void main(String[] args){
      Scanner reader = new Scanner(System.in);
  
      System.out.println( "\nHello, how many players would you like" );
      int playersNum = Integer.parseInt(Checks.userInputCheck( "\\d" ));
      System.out.println( "\nHow many cards should each player begin with" );
      int handSize = Integer.parseInt(Checks.userInputCheck( "\\d" ));
      System.out.println( "\nWhich strategy would you like to use 1 or 2" );
      int strategy = Integer.parseInt(Checks.userInputCheck( "[12]$" ));
  
      Logger.createDeck( playersNum, handSize );
  
      makePlayers( playersNum, handSize, strategy );
  
      dealInitialHand( playersNum, players, handSize );
  
      makePlayerOutputs();
  
      for ( int i = 0; i &lt; players.length; i++){
         logInitialHand(players[i]);
      }
  
      //for ( int i = 0; i &lt; players.length; i++){
        //  new Thread(players[i]).start();
     // }
     }
  
      private static void makePlayers(  int noPlayers, int noCardsInHand, int strategyChosen){
     players = new Player[noPlayers];
     for( int i = 0; i &lt; noPlayers; i++){
         players[i] = new Player( strategyChosen, noCardsInHand, i+1 );
         players[i].fillHand();
     }
  }
  
     private static void dealInitialHand(int noPlayers, Player[] players, int noCardsInHand ){
     System.out.println( "\nPlease enter the name of the deck file" );
     File theDeck = new File (Checks.userInputCheck( "deckfile.txt" ) );
     //BufferedReader fileReader = null;
     int intLine;
  
  
     try{
         //fileReader = new BufferedReader ( new FileReader( theDeck ) );
         Scanner fileScanner = new Scanner( theDeck );
  
         int line = 0;
  
         for( int h = 0; h &lt; noCardsInHand; h++){
             seePlayerHands();
             for( int p = 0; p &lt; noPlayers; p++){
                  System.out.println( "Player " + h + " Card" + p );
  
                  line = Integer.parseInt( fileScanner.nextLine() );
                  //seePlayerHands();
                  players[p].setHand( line, h );
                  //seePlayerHands();
             }
         }
     }catch (Exception e) {
         e.printStackTrace();
     }
  
     seePlayerHands();
      }
  
     private static void seePlayerHands(){
     for ( int i = 0; i &lt; players.length; i++){
        System.out.println( players[i].getPlayerName() + "'s hand is currently" );
        players[i].seeHand();
      }
      }
  </code></pre>
  
  <p>The player class</p>
  
  <pre><code>public class Player implements Runnable{
  Card[] hand;
  String playerName;
  int strategyChosen;
  
  public void run(){
      System.out.println( "les do dis" );
  }
  
  private Player(){
  }
  
  public Player( int strategy, int cardsInHand, int playerNumber ){
      hand = new Card[cardsInHand];
      strategyChosen = strategy;
      playerName = "Player " + playerNumber;
  }
  
  public String getPlayerName(){
      return playerName;
  }
  
  public void fillHand(){
      Card card = new Card(0);
  
      for ( int i = 0; i &lt; hand.length; i++){
          hand[i] = card;
      } 
  }
  
  public void setHand( int value, int index ){
      hand[index].setCardValue( value );
  }
  
  public void seeHand(){
      for ( int i = 0; i &lt; hand.length; i++){
          System.out.println( hand[i].getCardValue() );
      }
  }
  
  public int getHandValue( Card card, int handIndex ){
      return card.getCardValue( hand[handIndex] );
  }
  }
  </code></pre>
  
  <p>The card class</p>
  
  <pre><code>public class Card{
  static int cardValue;
  
  private Card(){
  }
  
  public Card( int value ){
      cardValue = value;
  }
  
  public void setCardValue( int value ){
      cardValue = value;
  }
  
  public static int getCardValue(){
      return cardValue;
  }
  
  public static int getCardValue( Card card ){
      return cardValue;
  }
  }
  </code></pre>
  
  <p>This is the result with the changes Ross suggested </p>
  
  <pre><code>Player 1's hand is currently
  0
  0
  0
  Player 2's hand is currently
  0
  0
  0
  Player 3's hand is currently
  0
  0
  0
  Player 0 Card0value5
  Player 1 Card0value3
  Player 2 Card0value6
  Player 1's hand is currently
  6
  6
  6
  Player 2's hand is currently
  6
  6
  6
  Player 3's hand is currently
  6
  6
  6
  Player 0 Card1value1
  Player 1 Card1value3
  Player 2 Card1value5
  Player 1's hand is currently
  5
  5
  5
  Player 2's hand is currently
  5
  5
  5
  Player 3's hand is currently
  5
  5
  5
  Player 0 Card2value3
  Player 1 Card2value3
  Player 2 Card2value2
  Player 1's hand is currently
  2
  2
  2
  Player 2's hand is currently
  2
  2
  2
  Player 3's hand is currently
  2
  2
  2
  </code></pre>
  
  <p>Thank you for the quick responses, hope this helps you to help me.</p>