Error type Mismatch: cannot Convert from String to String[] - How can I fix?

Asked

Viewed 763 times

0

When trying to receive value through a JOptionPane in a matrix, but gives this error:

type mismatch: cannot convert from String to String[]

How can I correct?

Follow my source code below:

package br.deivsoft.estudo.modelo;

import javax.swing.JOptionPane;

public class TestaAnalistas {

    public static void main(String[] args) {


        Analistas analist = new Analistas();

        analist.nome = JOptionPane.showInputDialog("Digite seu nome: ");
        analist.matricula = JOptionPane.showInputDialog("Digite sua matricula: ");
        analist.equipe = JOptionPane.showInputDialog("Digite sua equipe: ");


        analist.demandas = new String[3][4];

        for (int i = 0; i < analist.demandas.length; i++) {
            analist.demandas[i] = JOptionPane.showInputDialog("Digite o nome: ");
            for (int j = 0; j < analist.demandas[i].length; j++) {
                analist.demandas[i][j] = JOptionPane.showInputDialog("Digite a demanda: ");
            }
        }

        analist.exibirDadosAnalistas();

    }

}
package br.deivsoft.estudo.modelo;

public class Analistas {

    String nome;
    String matricula;
    String equipe;
    String[][] demandas;




    void exibirDadosAnalistas() {

        System.out.println("Nome: "+nome);
        System.out.println("Matricula: "+matricula);
        System.out.println("Equipe: "+equipe);

        for (int i = 0; i < demandas.length; i++) {
            System.out.println("Analista: "+demandas[i]);
            for (int j = 0; j < demandas[i].length; j++) {
                System.out.println("Demanda: "+demandas[i][j]);
            }
        }

    }


}

1 answer

5

Your mistake is here:

analist.demandas[i] = JOptionPane.showInputDialog("Digite o nome: ");

demandas[i] is an array line with several strings. You can’t put a single string there.

Your problem is deeper, your modeling is quite wrong. You have defined that a Analistas is a person with nome, matricula and equipe. So far so good, but that each Analistas has a two-dimensional demand matrix where the lines are also analysts and the matrix cells are the demands themselves.

I think what you wanted was to create an array of analysts and each analyst with their array of demands. This would make a lot more sense:

package br.deivsoft.estudo.modelo;

public class Analista {

    private String nome;
    private String matricula;
    private String equipe;
    private String[] demandas;

    public Analista(String nome, String matricula, String equipe, String[] demandas) {
        this.nome = nome;
        this.nome = matricula;
        this.nome = equipe;
        this.nome = demandas;
    }

    public void exibirDados() {
        System.out.println("Nome: " + nome);
        System.out.println("Matricula: " + matricula);
        System.out.println("Equipe: " + equipe);

        for (int i = 0; i < demandas.length; i++) {
            System.out.println("Demanda: " + demandas[i]);
        }
    }
}
package br.deivsoft.estudo.modelo;

import javax.swing.JOptionPane;

public class TestaAnalistas {

    public static void main(String[] args) {

        Analista[] analistas = new Analista[3];

        for (int i = 0; i < analistas.length; i++) {
            String nome = JOptionPane.showInputDialog("Digite seu nome:");
            String matricula = JOptionPane.showInputDialog("Digite sua matricula:");
            String nome = JOptionPane.showInputDialog("Digite sua equipe:");
            String[] demandas = new String[4];

            for (int j = 0; j < demandas; j++) {
                demandas[j] = JOptionPane.showInputDialog("Digite a demanda: ");
            }
            analistas[i] = new Analista(nome, matricula, nome, demandas);
        }

        for (Analista a : analistas) {
            a.exibirDados();
        }
    }
}

And finally some recommendations:

  • Never omit the modifiers public or private unless you are absolutely sure you want package visibility.

  • Class names in general are singular because each instance of it represents a single object.

  • Avoid name redundancy. In a class called Analista, the method name may be only exibirDados, since in the context, it is known that this is the data of the Analista. Hence, the name exibirDadosAnalistas is redundant.

  • If you have multiple analysts, then you will use an array of analysts. If each analyst has multiple demands, then it will have an array of demands within each analyst. Creating a demand matrix of all analysts will be something much more difficult, ugly and confusing and has far less chance of working.

  • Do not access object fields from other classes ever (except when one is inside the other, which is not your case). This is considered bad programming practice in Java.

Browser other questions tagged

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