Print more than 1 java value

3

I'm studying arrays. The matrix consists of character information from 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 is 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 problems with the method that returns the character of a particular season. The user types the desired season, and the method will print all the characters acting on it. I have the ready method already, I'm only having problem at the time of the 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 that the guy wants to know which characters are.

At the time of compiling the above method, without problems, however at the time of execution I have the following error:

Themostbizarre,isthatdespitetheerroritreturnstherightresult:

I just do not know why this error, which points to the following if:

if(mat[2][j].contains(temporada)) {
    System.out.println(mat[0][j] + "\n");
}

Of one thing I'm sure. It has to do with this System.out.println , because when I try to do this:

if(mat[2][j].contains(temporada)) {
    return mat[0][j];
}

Using return , it works perfectly, but only returns the first character, not all.

Update: Full Code below to improve viewing

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";
    }

}
    
asked by anonymous 16.05.2015 / 14:06

1 answer

2

The error is due to the fact that you declare the array as having 6 rows and 114 columns and only assign values to 6 rows and 2 columns.

The value returned by mat.length; is 6 and mat[i].length; is 114 . During the two loops i and j will point to elements of the array that have not been initialized, hence NullPointerException .

You should change the line:

String [] [] matriz = new String [6] [114];

To:

String [] [] matriz = new String [6] [2];

Errors aside, your matrix is also poorly formed, its dimensions should be reversed: [2] [6] . Each character should be assigned to a line and its characteristics assigned to the columns. On the other hand, you just need a loop to get the result you want. Given this your code would look like this:

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 [2] [6];
        matriz[0][0] = "Robin Arryn";
        matriz[0][1] = "Lino Facioli";
        matriz[0][2] = "145";
        matriz[0][3] = "Vivo";
        matriz[0][4] = "Arryn";
        matriz[0][5] = "Masculino";

        matriz[1][0] = "Yohn Royce";
        matriz[1][1] = "Rupert Vansittart";
        matriz[1][2] = "45";
        matriz[1][3] = "Vivo";
        matriz[1][4] = "Arryn";
        matriz[1][5] = "Masculino";
        return matriz;
    }
    public static String imprimeMatrizQualTemporada(String temporada, String [] [] mat) {
        for(int i = 0; i < mat.length; i++) {
            if(mat[i][2].contains(temporada)){
                System.out.println(mat[i][0] + "\n");
            }
        }
        return "Não encontramos um personagem com este nome";
    }
}

See on Ideone

    
16.05.2015 / 16:15