3
I’m studying matrices. The matrix is in characters information of the game of Thrones. My matrix:
public static String [][] matrizPrincipal() {
String [] [] matriz = new String [6] [114];
matriz[0][0] = "Robin Arryn";
matriz[1][0] = "Lino Facioli";
matriz[2][0] = "145";
matriz[3][0] = "Vivo";
matriz[4][0] = "Arryn";
matriz[5][0] = "Masculino";
matriz[0][1] = "Yohn Royce";
matriz[1][1] = "Rupert Vansittart";
matriz[2][1] = "45";
matriz[3][1] = "Vivo";
matriz[4][1] = "Arryn";
matriz[5][1] = "Masculino";
return matriz;
}
Yes, it’s not complete.
Linha 0: Nome Personagem
Linha 1: Ator
linha 2: Temporadas que participa
Linha 3: Vivo ou morto
Linha 4: Família
Linha 5: Genero
I’m having trouble with the method that returns the character of a certain season. The user types the desired season, and the method will print all the active characters in it. I have the method ready now, I’m only in trouble at the time of return.
Method:
public static String imprimeMatrizQualTemporada(String temporada, String [] [] mat) {
for(int i = 0; i < mat.length; i++) {
for(int j = 0; j < mat[i].length; j++) {
if(mat[2][j].contains(temporada)) {
System.out.println(mat[0][j] + "\n");
}
}
}
return "Não encontramos um personagem com este nome";
}
The String season and String mat comes from the main method, where season is the season the guy wants to know which characters are.
When compiling the above method, no problem, but when executing I have the following error:
The most bizarre, is that despite the error it returns the right result:
Just don’t know why this mistake, which points to the following if:
if(mat[2][j].contains(temporada)) {
System.out.println(mat[0][j] + "\n");
}
One thing I’m sure of. It has to do with this System.out.println
, 'Cause when I try to do it this way:
if(mat[2][j].contains(temporada)) {
return mat[0][j];
}
Using return
, it works perfectly, but only returns the first character, and not all.
Updating: Full code below to improve visualization
import java.util.Scanner;
public class matriz {
public static void main(String args[]) {
System.out.println("\f");
String [][] mat = matrizPrincipal();
System.out.println("1.Listar atores e suas respectivas temporadas.");
Scanner in = new Scanner(System.in);
String msg = in.nextLine();
int opcao = Integer.parseInt(msg);
switch(opcao) {
case 1:
System.out.println("Digite a temporada à se verificar");
String temporada = in.nextLine();
String resultado2 = imprimeMatrizQualTemporada(temporada,mat);
System.out.println("Personagens nesta Temporada:" +resultado2);
break;
}
}
public static String [][] matrizPrincipal() {
String [] [] matriz = new String [6] [114];
matriz[0][0] = "Robin Arryn";
matriz[1][0] = "Lino Facioli";
matriz[2][0] = "145";
matriz[3][0] = "Vivo";
matriz[4][0] = "Arryn";
matriz[5][0] = "Masculino";
matriz[0][1] = "Yohn Royce";
matriz[1][1] = "Rupert Vansittart";
matriz[2][1] = "45";
matriz[3][1] = "Vivo";
matriz[4][1] = "Arryn";
matriz[5][1] = "Masculino";
return matriz;
}
public static String imprimeMatrizQualTemporada(String temporada, String [] [] mat) {
for(int i = 0; i < mat.length; i++) {
for(int j = 0; j < mat[i].length; j++){
if(mat[2][j].contains(temporada)){
System.out.println(mat[0][j] + "\n");
}
}
}
return "Não encontramos um personagem com este nome";
}
}
Put the complete code instead of pieces. It is easier to reproduce and find the error.
– Renan Gomes
Ready! Aidicionei to the main post
– GGirotto
The code at the beginning of your question, the one that fills the matrix, is different from the one you put in the update. Which one is making the mistakes?
– ramaral
Are you talking about the main matrix method? Here’s the same as the.o
– GGirotto
Look at your question and you will find that they are not equal
– ramaral
Bizarre, I had not moved there. Anyway I already arranged..
– GGirotto
As it was initially I understood the errors, with the updated code I see nothing wrong.
– ramaral
I posted the error that appears in images just above. That’s why I think it is, because it shows this http error://i.stack.Imgur.com/Pfpwd.png but performs perfectly what was requested
– GGirotto