# Collecting data from an array

• I'm with a college exercise, but I'm lost in my logic and how to solve it. I have to collect the game results and set the score for each result. the winner takes 3 points, a draw guarantees 1 point and the defeat not from any point.

I know I don't have much yet but I wrote it here:

Obs: The teacher asks you to use only two-dimensional string arrays!

(Define a two-dimensional array of Strings representing the results of matches. and a four-element array of ints representing scores of teams of Germany, Ireland,Poland and Scotland (in this order). The program calculates total score for each team (3 points for a win, 1 for a draw, 0 for a defeat), puts them into the array and then prints it)

Obs2: this English only because I study and live in Poland and the course and in English.

public class Jogos {

public static void main(String[] args) {

String [][] resultados = new String[12][4];

for (int i=0; i&lt;resultados.length; i++){
for (int j=0; j&lt;resultados[i].length; j++){
System.out.print(resultados[i][j] + " - ");
}
System.out.println();
}

}
}

• If you can only use two-dimensional arrays Strings your solution is in the correct way, but the calculations of points are missing. If it's all one String then it will be necessary to interpret the numerical value of the goals with Integer.parseInt to know the result of each game.

He also missed an array for the scores as the statement indicates, which refers to teams: Germany, Ireland, Poland and Scotland in this exact order.

A possible solution would be:

public static int obterPosicao(String equipe){
switch (equipe){
case "Alemanha": return 0;
case "Irlanda": return 1;
case "Polonia": return 2;
case "Escocia": return 3;
}
return -1;
}

public static void main(String[] args) {
String[][] resultados = {
{"Alemanha","2","Escocia","1"},
{"Polonia","2","Alemanha","0"},
{"Alemanha","1","Irlanda","1"},
{"Polonia","2","Escocia","2"},
{"Escocia","1","Irlanda","0"},
{"Irlanda","1","Polonia","1"},
{"Irlanda","1","Escocia","1"},
{"Alemanha","3","Polonia","1"},
{"Escocia","2","Alemanha","3"},
{"Irlanda","1","Alemanha","0"},
{"Escocia","2","Polonia","2"},
{"Polonia","2","Irlanda","1"}
};

int[] pontuacoes = new int[4];
String[] nomes = {"Alemanha","Irlanda","Polonia","Escocia"};

for (int i = 0; i &lt; resultados.length; ++i){
int golos1 = Integer.parseInt(resultados[i][1]);//posicao 1 é golos de equipe1
int golos2 = Integer.parseInt(resultados[i][3]);//posicao 3 é golos de equipe2

int score1 = 1, score2 = 1; //assume empate por defeito
if (golos1 &gt; golos2){ //ajusta caso não seja
score1 = 3;
score2 = 0;
}
else if (golos1 &lt; golos2){
score1 = 0;
score2 = 3;
}

//obtem a posição da equipa no vetor pontuacoes com base no nome e ajusta score
}

for (int i = 0; i &lt; pontuacoes.length; ++i){
System.out.println("Pontuação para " + nomes[i] + " é " + pontuacoes[i]);
}

}

https://ideone.com/u30WLE

I have chosen to simplify some things, most notably the creation of the results. I also created an array of team names in scores to be simple to write the final results on the screen.

Certainly it would give to elaborate enough on the solution using HashMaps for example, but I suspect it was a solution of this nature that your teacher was waiting for.

2

2

2

2

2

2

2

2

2

2

2

2

2

2

2