3

What is the best way to print the cells of a String[][] array as a right-justified table? For example, the input

{ { "x", "xxx" }, { "yyy", "y" }, { "zz", "zz" } }

should yield the output

  x xxx
yyy   y
 zz  zz

This seems like something that one should be able to accomplish using java.util.Formatter, but it doesn't seem to allow non-constant field widths. The best answer will use some standard method for padding the table cells, not the manual insertion of space characters.

Chris Conway
  • 55,321
  • 43
  • 129
  • 155
  • Smells like homework - wording of the question is like an assignment? – The Archetypal Paul Nov 08 '08 at 22:57
  • Stackoverflow is for questions. This is an order. if you are going to try and have people do your homework for you, at least put the effort in to make it not look like it =) – Oli Nov 08 '08 at 23:03
  • 1) It's not homework. 2) The question is implicit: insert "how do I" in front of the text of the question. – Chris Conway Nov 08 '08 at 23:32
  • Have you even attempted it? What part are you struggling with? – TheSoftwareJedi Nov 08 '08 at 23:33
  • @TheSoftwareJedi The part where some printf-like method in the JDK does the hard work for me, instead of manually diddling with spaces. – Chris Conway Nov 08 '08 at 23:41
  • In my opinion, the question is reasonable. Certainly, he has the knowledge to create an algorithm doing what he requests for. At least you can't get 19.2k rep for only asking homework questions. He's just asking for the *best* way. Why reinvent the wheel if anybody knows a library? +1 for the question – Atmocreations Apr 22 '13 at 10:54

3 Answers3

8

Here's an answer, using dynamically-generated format strings for each column:

public static void printTable(String[][] table) {
  // Find out what the maximum number of columns is in any row
  int maxColumns = 0;
  for (int i = 0; i < table.length; i++) {
    maxColumns = Math.max(table[i].length, maxColumns);
  }

  // Find the maximum length of a string in each column
  int[] lengths = new int[maxColumns];
  for (int i = 0; i < table.length; i++) {
    for (int j = 0; j < table[i].length; j++) {
      lengths[j] = Math.max(table[i][j].length(), lengths[j]);
    }
  }

  // Generate a format string for each column
  String[] formats = new String[lengths.length];
  for (int i = 0; i < lengths.length; i++) {
   formats[i] = "%1$" + lengths[i] + "s" 
       + (i + 1 == lengths.length ? "\n" : " ");
 }

  // Print 'em out
  for (int i = 0; i < table.length; i++) {
    for (int j = 0; j < table[i].length; j++) {
      System.out.printf(formats[j], table[i][j]);
    }
  }
}
Chris Conway
  • 55,321
  • 43
  • 129
  • 155
4

Indeed, if you specify a width for the fields, it should be right-justified.
If you need to have a dynamic padding, minimal for the longest string, you have to walk the array, getting the maximal width, generate the format string with the width computed from this maxima, and use it for format the output.

PhiLho
  • 40,535
  • 6
  • 96
  • 134
1

find the length of the longest string..
left pad all the strings with spaces until they r that length + 1
System.out.print them using 2 nested for loops

Aditya Mukherji
  • 9,099
  • 5
  • 43
  • 49
  • Need to keep the length of the longest string in each column, probably, but otherwise close enough. Also, use printf to do the left-padding - question was tagged with printf. – The Archetypal Paul Nov 08 '08 at 22:59