Error type mismatch: Can not convert from String to String [] - How can I fix it?

0

When trying to receive value through JOptionPane in an array, but gives this error:

type mismatch: cannot convert from String to String[]

How can I fix it?

Here is 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]);
            }
        }

    }


}
    
asked by anonymous 12.01.2018 / 03:56

1 answer

4

Your error is here:

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

demandas[i] is a matrix row with multiple strings. You can not put a single string there.

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

I think what you wanted was to create an array of analysts and each analyst with your array of demands. That 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 modifiers public or private unless you are absolutely sure you want package visibility.

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

  • Avoid redundancy in the name. In a class called Analista , the method name can only be exibirDados , since by context, it is known that it is the Analista data. Therefore, the name exibirDadosAnalistas is redundant.

  • If you have multiple analysts, then you will use an array of analysts. If each analyst has several demands, then he will have an array with the demands within each analyst. Creating an array of demands from all analysts is going to be a lot harder, ugly, and confusing, and it has far less of a chance.

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

12.01.2018 / 04:28