2

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?

2
  • 7
    Iterate over the list and calculate the maximum string length of each attribute (as string). Then you can set the desired widths before you start displaying. Commented Jun 14, 2017 at 20:09
  • Welcome to Stack Overflow! Please take the tour, have a look around, and read through the help center, in particular How do I ask a good question? and What topics can I ask about here?. From that second link: "Questions asking for homework help must include a summary of the work you've done so far to solve the problem, and a description of the difficulty you are having solving it." - even if it is no homework. Commented Jun 14, 2017 at 20:10

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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("=");

        }

    }

}

1 Comment

This only ever compares the length to the first column and does not find the widths for each column.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.