title: Java Bowling Game - Referencing different objects
body: <p>OK, so I have a bowling game score calculator and in it I have the main class that reads input from the command line and create a "PlayerScore" object for each player, the PlayerScore object has an arraylist and a method to add to that arraylist, the game is supposed to go frame by frame and alternate between the players, asking for the amount of pins they knocked down in each frame and then calculating and generating a formatted score sheet, So for example I input that there will be two players, then I store the player names in a String ArrayList and then an empty playerscore object in a seperate cooresponding PlayerScore Arraylist, I have a for loop that alternates between the players nested in a for loop that keeps track of the current frame, So when I input the scores for one player it should reference their playerscore object, here is my code:</p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.FileNotFoundException;
  import java.io.FileReader;
  import java.io.FileWriter;
  import java.io.IOException;
  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.Scanner;
  import java.util.Set;
  /*
  * In each iteration call the add method of each players  PlayerScore object
  */
  
  public class BowlingGameCalculator {
      static int Frame = 1;
      private static String res;
      static ArrayList&lt;PlayerScore&gt; playerArrays = new ArrayList&lt;PlayerScore&gt;();
      //static HashMap&lt;String, PlayerScore&gt; play = new HashMap&lt;String, PlayerScore&gt;();
      static ArrayList&lt;String&gt; newscore = new ArrayList&lt;String&gt;();
      static ArrayList&lt;String&gt; players = new ArrayList&lt;String&gt;();
      static String[] scores = new String[21];
      static int rscore;
      static ArrayList&lt;Integer&gt; running_score = new ArrayList&lt;Integer&gt;();
  
      public static void main(String[] args) throws IOException {
          System.out.println("Enter the number of bowlers");
          Scanner in = new Scanner(System.in);
          int num = in.nextInt();
          for(int i =0; i&lt;num; i++) {
              System.out.println("enter bowlers name:");
              Scanner inp = new Scanner(System.in);
              String input = inp.next();
              PlayerScore scr = new PlayerScore();
              // play.put(input, scr);
              players.add(input);
              playerArrays.add(scr);
          }
          System.out.println(players);
          outerloop:
  
          for(int j=0; j&lt;players.size();) {
              for(int k=0; k&lt; 10;) {
                  if(j &gt; players.size()-1) {
                      j = j-players.size();
                      Frame++;
                  }
  
                  System.out.println("Frame : " + Frame);
                  if(Frame == 11) {
                      break outerloop;
                  }
                  String playr = players.get(j);
                  System.out.println("enter rolls for " + playr);
                  System.out.println("roll 1 : ");
  
                  Scanner inp = new Scanner(System.in);
                  int input = inp.nextInt();
                  if(input == 10) {
                      PlayerScore score = playerArrays.get(j);
                      System.out.println(score);
                      score.addRoll(input);
                      score.show();
                      //   PlayerScore scr = play.get(j);
                      // scr.addRoll(input);
                      j++;
                  }
                  else {
                      System.out.println("roll 2 :");
                      Scanner inp2 = new Scanner(System.in);
                      int input2 = inp2.nextInt();
                      PlayerScore score = playerArrays.get(j);
                      System.out.println(score);
                      score.addRoll(input);
                      score.addRoll(input2);
                      score.show();
                      //  PlayerScore scr = play.get(playr);
                      // scr.addRoll(input);
                      //scr.addRoll(input2);
                      // System.out.println(scr);
                      // scr.show();
                      //System.out.println(play);
                      j++;
                  }
              }
              //System.out.println(play);
          }
      }
  }
  </code></pre>
  
  <p>And here is the PlayerScore class</p>
  
  <pre><code>import java.util.ArrayList;
  public class PlayerScore {
      static ArrayList&lt;Integer&gt; score = new ArrayList&lt;Integer&gt;();
      static ArrayList&lt;Integer&gt; newscore = new ArrayList&lt;Integer&gt;();
      static ArrayList&lt;Integer&gt; running_score = new ArrayList&lt;Integer&gt;();
      public static int rscore;
  
      public PlayerScore() {
  
      }
      public PlayerScore(int[] in) {
  
      }
      public void addRoll(int x) {
          score.add(x);
      }
  
      //public boolean isComplete(){
  
      //}
      //public int getScore(){
  
      //}
  
      public ArrayList&lt;Integer&gt; show() {
          System.out.println(this.score);
          return this.score;
      }
  </code></pre>
  
  <p>now here is the output from a trial run</p>
  
  <pre><code>Enter the number of bowlers
  2
  enter bowlers name:
  matt
  enter bowlers name:
  derp
  [matt, derp]
  Frame : 1
  enter rolls for matt
  roll 1 : 
  5
  roll 2 :
  5
  PlayerScore@169ca65
  [5, 5]
  Frame : 1
  enter rolls for derp
  roll 1 : 
  5
  roll 2 :
  5
  PlayerScore@1301ed8
  [5, 5, 5, 5]
  Frame : 2
  enter rolls for matt
  roll 1 : 
  </code></pre>
  
  <p>Ok, so as you can see it adds the rolls [5,5] to the arraylist in matt's playscore object but then it adds the rolls to the same ArrayList which made me think that I was accidentally referencing the same object but as you can see I printed out the object ID's to the console and it shows that I am indeed calling different references of the PlayerScore object, it should be [5,5] for my score then [5,5] for derps score in frame 1, any help is appreciated </p>