Question:
Java Array Points System?
Elie
2012-12-17 11:46:25 UTC
Hey guys, I'm trying to learn Java programming as a pass time and I just had a question determining Arrays. I'm trying to build this game for my son's soccer team and I want to be able to keep track of all the scores.

What my goal is: I want to create an Array that can hold up to 12 integers. Those 12 integers will be considered like "Teams" if you will. When the program starts, I want to be able to select 2 of the Teams in the Array and I want to be able to add a value to them. So if team 1 scored 5 points and team 2 scored 3 points the program will take the greater number and assign +2 for team 1's score and if there is a tie both teams will receive +1 for their scores(I just want to keep track of who has the most points).

I'm learning how to save variables to a text file and open them again but it just the Array that is a little confusing to me.

I've tried to work on this but I can't even think where to start.
Your help is appreciated!
Three answers:
Jon T
2012-12-17 12:03:29 UTC
I do not recommend using a Java Array in the sense of String[] but rather use a java.util.Collection like a List interface and ArrayList concrete. Or even use a Map



Arrays (to include Collections) work by index. Programming indexes start at 0 and then go up. So an array of size 3 will have the indexes of 0, 1 & 2.



Normal Arrays:

To instantiate a normal Array you can do it one of two ways. Declaring the size with a number or declaring the size with content.

String[] teams = new String[12];

String[] teams = {"team one", "team two", "team three", ... };

To access the info you would do

teams[0]; // this is "team one"

teams[0] = "team one";



Collection List:

To instantiate a List you would declare it and then just add your elements. You can even use Generics to make sure the content is what you expect it to be

List teams = new ArrayList();

teams.get(0);

teams.add("team one");



Map:

I recommend this. A map has keys and values. The keys must be unique. In this case the key can be the string representation of your team and the value can be the integer score.

Map teams = new HashMap();

From here you can do

Integer score1 = teams.get("team one");

or

teams.put("team one", 5);



since key's are unique... if there is already a "team one" then the value is replaced, if there is not a "team one" then it is simply added to the Map.



I hope this gets you in the right direction
2012-12-17 12:10:24 UTC
I'll keep this simple. You will likely want to use a hash map, not a simple string array. The following is an example of how you would add and display team information:



http://ideone.com/vjssYQ



For storage in files I would recommend that you store the data in XML but this is up to you.
godfatherofsoul
2012-12-17 11:59:07 UTC
Sounds pretty straight forward. Create an int array 12 long. Make each team an "index" or a value from 0-11 on your array. You could even define a constant for each team name:



public static final int STEELERS=0;

public static final int RAVENS=1;

...



Then, your logic would look like:



int team1Score...

int team2Score...

int team1Index...

int team2Index...



if(team1Score>team2Score)

{

teamArray[team1Index]=teamArray[team1Index]+2;

}

else if (team1Score
...//do opposite for

else if (team1Score==team2Score)

{

teamArray[team1Index]=teamArray[team1Index]+1;

teamArray[team2Index]=teamArray[team2Index]+1;

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...