Display data from a matrix

0

I need help to show the data of a matrix, I use a variable a, which indicates the number of records (columns) it will have and 4 are the rows or fields it contains (identification, name, address, phone), The problem I have is that I need to enter only the identification and show the rest of the data that exists in that row.

    int a;
    a=Integer.parseInt(txt1.getText());
    String datos[][] = new String[a][4];

    for(int i=0;i<a;i++){
        JOptionPane.showMessageDialog(null,"Ha ingresado "+i+" datos");
        JOptionPane.showMessageDialog(null,"Ingrese los datos en el siguiente orden \nCedula\nNombre\nDireccion\nTelefono");
        for(int j=0;j<4;j++){
            datos[i][j]=JOptionPane.showInputDialog(null,"Ingrese los datos");
        }

    }
    JOptionPane.showMessageDialog(null,"Busqueda de los datos ingresados");
    
asked by Kevin Velasco 22.01.2018 в 03:26
source

2 answers

0

First of all, I do not understand well how are you trying to put the data in the matrix since in your description you decide that each record goes in one column and each row is a field, however in the code you are putting each record as a row and that the columns are each field.

Now to show a record from its identification it is enough to carry out a search in the first column of the matrix of that identifier:

String identificador = JOptionPane.showInputDialog(null, "Ingrese identificador");
boolean encontrado = false; //Hago uso de una bandera para cortar el while
int pos = 0;

while(!encontrado && pos < a){
    //Pregunto si la cadena de esa posicion es igual a identificador
    if(identificador.equals(datos[pos][0]))
        encontrado = true;   //Encontré la fila
    else
        pos++;
}

//Si la posición es válida
if(pos < a){
    for(int j=0; j<4; j++)
        JOptionPane.showMessageDialog(null, datos[pos][j]);
}
else{
    JOptionPane.showMessageDialog(null, "El identificador ingresado no existe");
}
    
answered by 22.01.2018 в 06:55
0

If you want to search only the identification, you should do it in position 0 of each iteration. (commented code)

JOptionPane.showMessageDialog(null,"Busqueda de los datos ingresados");
//Ingresamos el código cedula a buscar
String code = JOptionPane.showInputDialog(null,"Ingrese cédula a buscar");
int pos= -1; // Var temporal
//Iteramos el array
for (int i = 0; i < datos.length; i++) {
    // Sí en la fila i , la posición fija 0 porque 
    // es donde se guarda la cédula
    if(datos[i][0].equals(code)){
        //asignamos la posición de la fila
        pos = i;
        // cortamos el ciclo
        break;
    }   
}
//si es diferente , quiere decir que si lo encontró
if(pos!= -1){
    // Imprimimos el array , podría hacerlo campo por campo con datos[pos][0]
    JOptionPane.showMessageDialog(null,Arrays.toString(datos[pos]));
}else{
    JOptionPane.showMessageDialog(null,"No existe el Usuario con ese Número de Cédula");
}

Yes, it uses Java8 , making use of the class Optional and of the streams we could achieve this goal. filtering the array (filter) using lambdas , with condition identificador.equals(x[0]) referring to the first field of array . If you find element isPresent() will return true and optional.get() will get the array that met the condition.

Optional<String[]> optional = Arrays.stream(datos)
                               .filter(x -> identificador.equals(x[0]))
                               .findFirst();
if(optional.isPresent()) {
    String[] data = optional.get();
    for (int i = 0; i < data.length; i++) {
        JOptionPane.showMessageDialog(null,"Campo : " + (i)+ data[i]);
    }
}else{
    JOptionPane.showMessageDialog(null, "No se encuentra el Identificador " + identificador);
}
    
answered by 22.01.2018 в 06:41