Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8193673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:33:47+00:00 2026-06-07T04:33:47+00:00

I’m looking for a util which will print a rectangular String[][] into a human-readable

  • 0

I’m looking for a util which will print a rectangular String[][] into a human-readable table with correct column lengths.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T04:33:49+00:00Added an answer on June 7, 2026 at 4:33 am

    If you want something similar to MySQL command-line client output, you can use something like that:

    import java.io.PrintStream;
    
    import static java.lang.String.format;
    import static java.lang.System.out;
    
    public final class PrettyPrinter {
    
        private static final char BORDER_KNOT = '+';
        private static final char HORIZONTAL_BORDER = '-';
        private static final char VERTICAL_BORDER = '|';
    
        private static final String DEFAULT_AS_NULL = "(NULL)";
    
        private final PrintStream out;
        private final String asNull;
    
        public PrettyPrinter(PrintStream out) {
            this(out, DEFAULT_AS_NULL);
        }
    
        public PrettyPrinter(PrintStream out, String asNull) {
            if ( out == null ) {
                throw new IllegalArgumentException("No print stream provided");
            }
            if ( asNull == null ) {
                throw new IllegalArgumentException("No NULL-value placeholder provided");
            }
            this.out = out;
            this.asNull = asNull;
        }
    
        public void print(String[][] table) {
            if ( table == null ) {
                throw new IllegalArgumentException("No tabular data provided");
            }
            if ( table.length == 0 ) {
                return;
            }
            final int[] widths = new int[getMaxColumns(table)];
            adjustColumnWidths(table, widths);
            printPreparedTable(table, widths, getHorizontalBorder(widths));
        }
    
        private void printPreparedTable(String[][] table, int widths[], String horizontalBorder) {
            final int lineLength = horizontalBorder.length();
            out.println(horizontalBorder);
            for ( final String[] row : table ) {
                if ( row != null ) {
                    out.println(getRow(row, widths, lineLength));
                    out.println(horizontalBorder);
                }
            }
        }
    
        private String getRow(String[] row, int[] widths, int lineLength) {
            final StringBuilder builder = new StringBuilder(lineLength).append(VERTICAL_BORDER);
            final int maxWidths = widths.length;
            for ( int i = 0; i < maxWidths; i++ ) {
                builder.append(padRight(getCellValue(safeGet(row, i, null)), widths[i])).append(VERTICAL_BORDER);
            }
            return builder.toString();
        }
    
        private String getHorizontalBorder(int[] widths) {
            final StringBuilder builder = new StringBuilder(256);
            builder.append(BORDER_KNOT);
            for ( final int w : widths ) {
                for ( int i = 0; i < w; i++ ) {
                    builder.append(HORIZONTAL_BORDER);
                }
                builder.append(BORDER_KNOT);
            }
            return builder.toString();
        }
    
        private int getMaxColumns(String[][] rows) {
            int max = 0;
            for ( final String[] row : rows ) {
                if ( row != null && row.length > max ) {
                    max = row.length;
                }
            }
            return max;
        }
    
        private void adjustColumnWidths(String[][] rows, int[] widths) {
            for ( final String[] row : rows ) {
                if ( row != null ) {
                    for ( int c = 0; c < widths.length; c++ ) {
                        final String cv = getCellValue(safeGet(row, c, asNull));
                        final int l = cv.length();
                        if ( widths[c] < l ) {
                            widths[c] = l;
                        }
                    }
                }
            }
        }
    
        private static String padRight(String s, int n) {
            return format("%1$-" + n + "s", s);
        }
    
        private static String safeGet(String[] array, int index, String defaultValue) {
            return index < array.length ? array[index] : defaultValue;
        }
    
        private String getCellValue(Object value) {
            return value == null ? asNull : value.toString();
        }
    
    }
    

    And use it like that:

    final PrettyPrinter printer = new PrettyPrinter(out);
    printer.print(new String[][] {
            new String[] {"FIRST NAME", "LAST NAME", "DATE OF BIRTH", "NOTES"},
            new String[] {"Joe", "Smith", "November 2, 1972"},
            null,
            new String[] {"John", "Doe", "April 29, 1970", "Big Brother"},
            new String[] {"Jack", null, null, "(yes, no last name)"},
    });
    

    The code above will produce the following output:

    +----------+---------+----------------+-------------------+
    |FIRST NAME|LAST NAME|DATE OF BIRTH   |NOTES              |
    +----------+---------+----------------+-------------------+
    |Joe       |Smith    |November 2, 1972|(NULL)             |
    +----------+---------+----------------+-------------------+
    |John      |Doe      |April 29, 1970  |Big Brother        |
    +----------+---------+----------------+-------------------+
    |Jack      |(NULL)   |(NULL)          |(yes, no last name)|
    +----------+---------+----------------+-------------------+
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
I would like to count the length of a string with PHP. The string
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.