How to make an ArrayList and as a result of the program I will paint them as a table!

1

I have a program in java using ArrayList, the detail that I want as a result I would paint them as if it were a table, that is:

Nombre|ApellidoMaterno |ApellidoPaterno 
Juan  |Bustamante      |Solis   

And so on

This is my code:

public class Empleado1 {

    public int IdEmpleado;
    public String Nombre;
    public String ApellidoP;
    public String ApellidoM;

    public Empleado1(int IdEmpleado, String Nombre, String ApellidoP, String ApellidoM){
        this.IdEmpleado = IdEmpleado;
        this.Nombre = Nombre;
        this.ApellidoP = ApellidoP;
        this.ApellidoM = ApellidoM;

    }

    //Metodo para imprimir el array de Empleado

    static void imprimeArrayEmpleados(Empleado1[] array){
        for(int i=0; i< array.length; i++){
            System.out.println("ID " + array[i].IdEmpleado + " Nombre: " + array[i].Nombre + "|" + "Apellido Paterno: " + array[i].ApellidoP + "|" + "Apellido Materno: "  + array[i].ApellidoM);

        }

    }



}

And this is the main class:

public static void main(String[] args) {
        Empleado1[] arrayEmpledos = new Empleado1[11];
        arrayEmpledos[0] = new Empleado1(1, " Armando ", "Palafox", "Martinez");
        arrayEmpledos[1] = new Empleado1(2, " Juan Jose", "Bustamante", "Solis");
        arrayEmpledos[2] = new Empleado1(3, " Armando ", "Palafox", "Martinez");
        arrayEmpledos[3] = new Empleado1(4, " Juan Jose", "Bustamante", "Solis");
        arrayEmpledos[4] = new Empleado1(5, " Armando ", "Palafox", "Martinez");
        arrayEmpledos[5] = new Empleado1(6, " Juan Jose", "Bustamante", "Solis");
        arrayEmpledos[6] = new Empleado1(7, " Armando ", "Palafox", "Martinez");
        arrayEmpledos[7] = new Empleado1(8, " Juan Jose", "Bustamante", "Solis");
        arrayEmpledos[8] = new Empleado1(9, " Armando ", "Palafox", "Martinez");
        arrayEmpledos[9] = new Empleado1(10, " Juan Jose", "Bustamante", "Solis");
        arrayEmpledos[10] = new Empleado1(13, " Armando ", "Palafox", "Martinez");


        imprimeArrayEmpleados(arrayEmpledos);

    }

I hope and you can help me to give me a tip of how I could do it, greetings friends.

    
asked by Juan José Bustamante 25.01.2018 в 23:30
source

2 answers

1

You can do that using the class Formatter .

System.out.println("ID  |Nombre         |Apellido paterno |Apellido materno");
System.out.println("----|---------------|-----------------|---------------");
for(int i=0; i< arrayEmpledos.length; i++){
    System.out.println(new Formatter().format("%-4s", arrayEmpledos[i].id)
                  +""+ new Formatter().format("%-16s", "| " + arrayEmpledos[i].nombre)
                     + new Formatter().format("%-16s", "| " + arrayEmpledos[i].apellidoP)
                     + new Formatter().format("%-16s", "  | " + arrayEmpledos[i].apellidoM));
}

In operation of the Formatter class is simple, the first parameter of the constructor receives the expression that is used to format the text and the second parameter the text to be formatted. In this case the expression is simple "%-4s" : - aligns the text to the left; 4 number of spaces that the text will have; s can be applied to any type of argument (text, numeric, floating, etc.).

    
answered by 26.01.2018 / 01:00
source
0

You can do something like this:

public static void main(String[] args) {
        System.out.format("%10s%20s%20s", "NOMBRE", "APELLIDO PATERNO", "APELLIDO MATERNO");
        for(int i = 0; i<10; i++){
            String nombre = "Juan"+i;
            String apellidoP = "Lopez"+i;
            String apellidoM = "Lopez"+(i+1);
            System.out.println("\n");
            System.out.format("%10s%10s%20s", nombre, apellidoP, apellidoM);
            System.out.println("\n");
        }
    }

I leave the documentation .

    
answered by 26.01.2018 в 00:34