Print more than 1 java value

Asked

Viewed 184 times

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:

Erro na hora de executar

The most bizarre, is that despite the error it returns the right result:

Resultado sendo exibido

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.

  • Ready! Aidicionei to the main post

  • 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?

  • Are you talking about the main matrix method? Here’s the same as the.o

  • Look at your question and you will find that they are not equal

  • Bizarre, I had not moved there. Anyway I already arranged..

  • As it was initially I understood the errors, with the updated code I see nothing wrong.

  • 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

Show 3 more comments

1 answer

2


The error is due to the fact that you declare the matrix to have 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.
In the course of both loops iand j will point to matrix elements that have not been initialized, hence the NullPointerException.

You should change the line:

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

To:

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

Mistakes aside, also its matrix is poorly formed, its dimensions should be reversed:[2] [6].
Each character should be assigned to a row and its characteristics assigned to the columns.
On the other hand only one is needed loop to get the result you want.
Given this your code would be 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

  • Perfect was just that! I defined 114 because they will have a total 114 lines, just wanted to leave the boring part for the final rsrs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.