Milos Rasevic
Milos Rasevic

Reputation: 43

Printing an ArrayList of Objects as a table with non-fixed size in Java

I have a list of objects, and I need to print it as a table where first row is the header and each row after is one object, and each column in the row represents one attribute. And I need the table to adjust its size by the size of the text in each field. For example, I need something like this:

    =============================================
    | First Name  |   Last Name    |     Age    |
    =============================================
    |Person's name|Person's surname|Person's age|

to change size if the text in the Field "First Name" gets bigger, like this:

    =======================================================
    |       First Name      |   Last Name    |     Age    |
    =======================================================
    |Person's very long name|Person's surname|Person's age|

Is it possible to manage this and how?

Upvotes: 2

Views: 1423

Answers (3)

Java Devil
Java Devil

Reputation: 10959

I'm going to assume you have something like a Person object like this

public class Person
{
     public String fName;
     public String lName;
     public String age;
}
  1. Implement your own list which will keep a track of the widths as you add elements to it, something like (very crude example)

    public class MyList<T extends Person> extends ArrayList<T>
    {
        public int[] colWidths = new int[3];
    
        @Override
        public boolean add(T e)
        {
             colWidths[0] = (colwidths[0] > e.fName.length()) ? colwidths[0] : e.fName.length();
             colWidths[1] = (colwidths[1] > e.lName.length()) ? colwidths[1] : e.lName.length();
             colWidths[2] = (colwidths[2] > e.age.length()) ? colwidths[2] : e.age.length();
    
             return super.add(e);
        }
    }
    
  2. Iterate your list to calculate the max widths

    public int[] colWidths = new int[3];
    for(Person p : yourList)
    {
             colWidths[0] = (colwidths[0] > p.fName.length()) ? colwidths[0] : p.fName.length();
             colWidths[1] = (colwidths[1] > p.lName.length()) ? colwidths[1] : p.lName.length();
             colWidths[2] = (colwidths[2] > p.age.length()) ? colwidths[2] : p.age.length();
    }
    

    The obvious down side of this second approach is that you will need to iterate your list twice.

Then define a print method using these max widths (for example)

public void printMyList(List<Person> aList, int[] maxColWidths)
{
     // Do your printing
}

This question should help out with a method to format a string centered if required.

Upvotes: 1

zypa
zypa

Reputation: 303

I agree with Rudy Velthuis above. The code should iterate in order to get the biggest value of the String, then paint the box around the text. Should be like this:

import java.util.Scanner;

public class GettingBiggerName {

    static String firstName, secondName, thirdName; // testing with just 3 Strings that will be inserted in an Array

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        System.out.print("Enter first name: ");
        firstName = in.nextLine();

        System.out.print("Enter second name: ");
        secondName = in.nextLine();

        System.out.print("Enter third name: ");
        thirdName = in.nextLine();

        System.out.println();

        String[] arrayOne = { firstName, secondName, thirdName }; // Created the array with the 3 strings for testing purpose

        int count=0; int progress = 0;
        for (int i = 0; i < arrayOne.length; i++) { // iterating to get the biggest length of the Strings inside the array

            if (arrayOne[i].length() > arrayOne[progress].length()) {
                count = arrayOne[i].length();
                progress++;
            }

            else {
                count = arrayOne[progress].length();
            }

        }

        System.out.println("Printing the header of the box: ");

        // printing the box to fit the length of the biggest String inside the array
        for (int i = 0; i < count; i++) {
            System.out.print("=");

        }

    }

}

Upvotes: 0

Syn
Syn

Reputation: 776

You would need the maximum length for each column beforehand. Then you can adapt your table header accordingly and start printing. Sadly I do not know of any other way to pretty-print this to the console.

Upvotes: 0

Related Questions