What is the significance of the print table Java?

335    Asked by AnilJha in Java , Asked on Oct 12, 2022

This program prints out a table in the console. Suggestions for improvements are welcome.


Example output
Name             Sex      Age
Klaus Ulbrecht   male     12
Dieter           male     14
Ursula           female   16
You can test and modify the program here.
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
class Main {
  private static List> table; 
  public static void main(String[] args) {
    initTable();
    int spacing = 3;
    printTable(spacing);
  }
  private static void initTable() {
    List row1 = Arrays.asList("Name", "Klaus Ulbrecht", "Dieter", "Ursula");
    List row2 = Arrays.asList("Sex", "male", "male", "female");
    List row3 = Arrays.asList("Age", "12", "14", "16");
    table = Arrays.asList(row1, row2, row3);
  }


  private static void printTable(int spacing) {
    List maxLengths = findMaxLengths();
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < table>      for (int j = 0; j < table>        String currentValue = table.get(j).get(i);
        sb.append(currentValue);
        for (int k = 0; k < (maxLengths.get(j) - currentValue.length() + spacing); k++) {
          sb.append(' ');
        }
      }
      sb.append('n');
    }
    System.out.println(sb);
  }
  private static List findMaxLengths() {
    List maxLengths = new ArrayList<>();
    for (List row : table) {
      int maxLength = 0;
      for (String value : row) {
        if (value.length() > maxLength) {
          maxLength = value.length();
        }
      }
      maxLengths.add(maxLength);
    }
    return maxLengths;
  }
}
Answered by Amit verma

Printing arbitrary data as a print table java is a nice task of work for a utility function. Such utility functions are usually defined in a utility class, which looks like this:

public final class Tables {
    private Tables() {}
    public static void print(List> table, int spacing) {
        ...
    }
}

The name of the utility class usually takes the plural form of the main data ingredient. In this case that is a table, therefore Tables. The Java programming environment already defines similar utility classes named Collections and Arrays. There is one important difference to your current code. All the variables needed by the Tables.print method are passed via parameters. There is no static field anywhere. This allows the print method to be called several times in the same moment and unambiguously lists all the data you have to provide when calling that method.

Citing from your code:
private static void initTable() {
  List row1 = Arrays.asList("Name", "Klaus Ulbrecht", "Dieter", "Ursula");
  List row2 = Arrays.asList("Sex", "male", "male", "female");
  List row3 = Arrays.asList("Age", "12", "14", "16");
  table = Arrays.asList(row1, row2, row3);}

You are mixing up the terms row and column here. A row in initTable will later be output as a column in printTable. That's confusing.

Several parts of your printTable method should be changed:

The table should be passed as a parameter, instead of being a static field in the Main class.

In the innermost for loop, the complicated expression is calculated several times, which is unnecessary. You should rather count from k downto 0: for (int k = ...; k > 0; k--) { ... }

The final System.out.println prints 2 newlines. One of them may or may not be desired. In the latter case, replace the println with print.

In the findMaxLengths method:

The table should be passed as a parameter, as above.

Instead of the if clause, you can just write maxLength = Math.max(maxLength, value.length());



Your Answer

Interviews

Parent Categories