Reputation: 13
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:
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<PlayerScore> playerArrays = new ArrayList<PlayerScore>();
//static HashMap<String, PlayerScore> play = new HashMap<String, PlayerScore>();
static ArrayList<String> newscore = new ArrayList<String>();
static ArrayList<String> players = new ArrayList<String>();
static String[] scores = new String[21];
static int rscore;
static ArrayList<Integer> running_score = new ArrayList<Integer>();
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<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<players.size();) {
for(int k=0; k< 10;) {
if(j > 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);
}
}
}
And here is the PlayerScore class
import java.util.ArrayList;
public class PlayerScore {
static ArrayList<Integer> score = new ArrayList<Integer>();
static ArrayList<Integer> newscore = new ArrayList<Integer>();
static ArrayList<Integer> running_score = new ArrayList<Integer>();
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<Integer> show() {
System.out.println(this.score);
return this.score;
}
now here is the output from a trial run
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 :
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
Upvotes: 0
Views: 4366
Reputation: 7894
You have declared the score variable as static in the PlayerScore class which means it is a class variable - there is only one which will be shared by the two PlayerScore objects you have created.
http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html
Upvotes: 0
Reputation: 285405
Upvotes: 3