how can I solve the String problem can not be converted to String []?

2
public ConsPacientesInternalFrame() {            
    initComponents();
    gestorpacientesControl = new controlador.GestorPacienteControl(this);
    String titulosTabla[] = ("identificacion","Nombres","Apellidos","FechaNac","Sexo");// esta linea tiene el error
    tabla = new DefaultTableModel(null,titulosTabla);
    ResultadosTbl.setModel(tabla);
    AceptarBtn.addActionListener(gestorpacientesControl);  
}

The line

String titulosTabla[] = ("identificacion","Nombres","Apellidos","FechaNac","Sexo");

Shows an error indicating that:

  

String can not be converted to String []

    
asked by Leonardo T 15.10.2018 в 19:42
source

2 answers

0

The creation and initialization of an array has this syntax (note that you use keys { and not parentheses ( ):

String titulosTabla[] = new String[]{"identificación", "nombres"};

o:

String titulosTabla[] = {"identificación", "nombres"};

This last form can only be used in the declaration of local attributes or variables:

String titulosTabla[]; // declaración
titulosTabla = {"identificación", "nombre"}; // Error no es una declaración
titulosTabla = new String[]{"identificación", "nombres"}; // Correcto
    
answered by 15.10.2018 / 20:00
source
0

The error is that you are initializing the array using parentheses.

The documentation indicates that you must use keys { ... } if you want to create an initialized array object.

This is the example of the documentation:

String[] aas    = { "array", "of", "String", };

You should therefore create your array like this:

String titulosTabla[] = {"identificacion","Nombres","Apellidos","FechaNac","Sexo"};

and problem solved.

A curious fact

If you look at the image, taken from the documentation:

they put the [] that indicate that it is an array in some cases after the identifier of the variable, in other cases after the identifier . I do not know if that corresponds to some kind of convention, I've tried it in both ways and it works:

    String[] titulosTabla = {"identificacion","Nombres","Apellidos","FechaNac","Sexo"};// esta linea tiene el error

Or:

    String titulosTabla[]= {"identificacion","Nombres","Apellidos","FechaNac","Sexo"};// esta linea tiene el error
    
answered by 15.10.2018 в 20:08