2
\$\begingroup\$

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 < 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           |                    |
----------------------------------------------------------------
\$\endgroup\$
1
\$\begingroup\$

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 < 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 < theArray.length; i+=col) {
        System.out.println(repeat(len+col+1,"-"));
        System.out.print("|");
        for (int c = 0; c < 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 < 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, "-"));
| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ output example please \$\endgroup\$ – Daniel Brose Apr 6 '16 at 0:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.