Collect the data entered in a single JOptionPane

0

I would like to know how I have to do to gather in a single JOptionPane all the values that I enter by keyboard and not one by one. I am starting to use the windows of the JOptionPane and I have not finished clarifying at all. Thanks!

import javax.swing.JOptionPane; public class NumeroTeclado {

public static void main(String[] args) {

    int [] dato = new int [10];
    int num = 1;

    for(int i = 0; i<dato.length; i++) {

        dato[i] = Integer.parseInt( JOptionPane.showInputDialog("Introduzca el valor " +num));
        num++;
    }
        num = 1;
    for(int i = 0; i<dato.length; i++) {

        JOptionPane.showMessageDialog(null, "El valor de la posición " + num + " es: " + dato[i]);
        num++;
    }
}

}

    
asked by Mario 24.11.2018 в 00:03
source

1 answer

1

simply concatenates the data read to a String changes your code in the Loop For

num = 1;
for(int i = 0; i<dato.length; i++) {
JOptionPane.showMessageDialog(null, "El valor de la posición " + num + " es: " + dato[i]);
num++;
}

as follows:

 String mensaje = "Los Valores Ingresados son:\n";
 for (int i = 0; i<dato.length; i++) {
     mensaje += String.format("valor en %d es %d\n",i+1, dato[i]) ;
 }
 JOptionPane.showMessageDialog(null, mensaje,"Informacion", JOptionPane.INFORMATION_MESSAGE);

Another suggestion you can improve and avoid using the variable num if you do this:

change:

dato[i] = Integer.parseInt( JOptionPane.showInputDialog("Introduzca el valor " +num));

by:

dato[i] = Integer.parseInt(JOptionPane.showInputDialog("Introduzca el valor " + (i+1)));
    
answered by 25.11.2018 / 08:11
source