Displaying a String array in tabular format I have an array of strings called theArray. All strings in theArray are of length 20. I wish to display this array in tabular format to view the clustering of a hashing function. This code works, however, it looks horrible. Is there a better way to create tables?

       for (int i = 0; i &lt; arraySize; i+=3) {

       System.out.println("----------------------------------------------------------------");
       System.out.println("|        " + i + "          |        " + (i + 1) +  "        |        " + (i + 2) +  "         |");
       System.out.println("----------------------------------------------------------------");
       System.out.println("| " + theArray[i] + "|" + theArray[i+1] + "|" + theArray[i+2]+"|");
   }


When this runs, it looks like this:

----------------------------------------------------------------
|         21           |         22        |        23          |
----------------------------------------------------------------
|                     |Finagle             |deciduous           |
----------------------------------------------------------------
|         24           |         25        |        26          |
----------------------------------------------------------------
| perennial           |condition           |                    |
----------------------------------------------------------------
First you should use a Method for repeating a character/String (if there isn't one yet. Don't know, never need it so far). Like a repeat-Method ->

    public String repeat(int times, String str) {
        // Add some security checks ofc, just to show what I mean.
        String r = "";
        for (int i=0; i &lt; times; i++) r += str;
        return r;
    }


So, since multible characters won't be a problem now anymore:

    // ...
    // Amount of columns you want to have
    int col = 3;
    // Maximal Length of a String you support
    int len = 20;
    for (int i=0; i &lt; theArray.length; i+=col) {
        System.out.println(repeat(len+col+1,"-"));
        System.out.print("|");
        for (int c = 0; c &lt; i; c++)
            // Add some checks to align it into the center when you want/need it
            System.out.print(i + repeat(len-(i+"".length()), " ") + "|");
        System.out.print("\n");
        System.out.println(repeat(len+col+1, "-"));
        for (int c = 0; c &lt; i; c++)
            // Filling up the rest of the String with spaces so the table wont freak out
            System.out.print(theArray[i+c] + repeat(len-theArray[i+c].length(), " ") + "|");
        System.out.print("\n");
    }
    System.out.println(repeat(len+col+1, "-"));