@Dexter1997/

Textual Table View

Java

This program prints out a table.

fork
loading
Files
  • Main.java

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.3840839745527085","path":"Main.java","file":{"path":"Main.java","content":{"asEncoding":{"base64":"aW1wb3J0IGphdmEudXRpbC5BcnJheXM7CmltcG9ydCBqYXZhLnV0aWwuTGlzdDsKaW1wb3J0IGphdmEudXRpbC5BcnJheUxpc3Q7CgpjbGFzcyBNYWluIHsKICBwcml2YXRlIHN0YXRpYyBMaXN0PExpc3Q8U3RyaW5nPj4gdGFibGU7IAoKICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmdbXSBhcmdzKSB7CiAgICBpbml0VGFibGUoKTsKICAgIGludCBzcGFjaW5nID0gMzsKICAgIHByaW50VGFibGUoc3BhY2luZyk7CiAgfQoKICBwcml2YXRlIHN0YXRpYyB2b2lkIGluaXRUYWJsZSgpIHsKICAgIExpc3Q8U3RyaW5nPiByb3cxID0gQXJyYXlzLmFzTGlzdCgiTmFtZSIsICJLbGF1cyBVbGJyZWNodCIsICJEaWV0ZXIiLCAiVXJzdWxhIik7CiAgICBMaXN0PFN0cmluZz4gcm93MiA9IEFycmF5cy5hc0xpc3QoIlNleCIsICJtYWxlIiwgIm1hbGUiLCAiZmVtYWxlIik7CiAgICBMaXN0PFN0cmluZz4gcm93MyA9IEFycmF5cy5hc0xpc3QoIkFnZSIsICIxMiIsICIxNCIsICIxNiIpOwogICAgdGFibGUgPSBBcnJheXMuYXNMaXN0KHJvdzEsIHJvdzIsIHJvdzMpOwogIH0KCiAgcHJpdmF0ZSBzdGF0aWMgdm9pZCBwcmludFRhYmxlKGludCBzcGFjaW5nKSB7CiAgICBMaXN0PEludGVnZXI+IG1heExlbmd0aHMgPSBmaW5kTWF4TGVuZ3RocygpOwoKICAgIFN0cmluZ0J1aWxkZXIgc2IgPSBuZXcgU3RyaW5nQnVpbGRlcigpOwogICAgZm9yIChpbnQgaSA9IDA7IGkgPCB0YWJsZS5nZXQoMCkuc2l6ZSgpOyBpKyspIHsKICAgICAgZm9yIChpbnQgaiA9IDA7IGogPCB0YWJsZS5zaXplKCk7IGorKykgewogICAgICAgIFN0cmluZyBjdXJyZW50VmFsdWUgPSB0YWJsZS5nZXQoaikuZ2V0KGkpOwogICAgICAgIHNiLmFwcGVuZChjdXJyZW50VmFsdWUpOwogICAgICAgIGZvciAoaW50IGsgPSAwOyBrIDwgKG1heExlbmd0aHMuZ2V0KGopIC0gY3VycmVudFZhbHVlLmxlbmd0aCgpICsgc3BhY2luZyk7IGsrKykgewogICAgICAgICAgc2IuYXBwZW5kKCcgJyk7CiAgICAgICAgfQogICAgICB9CiAgICAgIHNiLmFwcGVuZCgnXG4nKTsKICAgIH0KCiAgICBTeXN0ZW0ub3V0LnByaW50bG4oc2IpOwogIH0KCiAgcHJpdmF0ZSBzdGF0aWMgTGlzdDxJbnRlZ2VyPiBmaW5kTWF4TGVuZ3RocygpIHsKICAgIExpc3Q8SW50ZWdlcj4gbWF4TGVuZ3RocyA9IG5ldyBBcnJheUxpc3Q8PigpOwogICAgZm9yIChMaXN0PFN0cmluZz4gcm93IDogdGFibGUpIHsKICAgICAgaW50IG1heExlbmd0aCA9IDA7CiAgICAgIGZvciAoU3RyaW5nIHZhbHVlIDogcm93KSB7CiAgICAgICAgaWYgKHZhbHVlLmxlbmd0aCgpID4gbWF4TGVuZ3RoKSB7CiAgICAgICAgICBtYXhMZW5ndGggPSB2YWx1ZS5sZW5ndGgoKTsKICAgICAgICB9CiAgICAgIH0KICAgICAgbWF4TGVuZ3Rocy5hZGQobWF4TGVuZ3RoKTsKICAgIH0KICAgIHJldHVybiBtYXhMZW5ndGhzOwogIH0KfQ=="},"asBuffer":null},"loaded":true}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;

class Main {
  private static List<List<String>> table; 

  public static void main(String[] args) {
    initTable();
    int spacing = 3;
    printTable(spacing);
  }

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

  private static void printTable(int spacing) {
    List<Integer> maxLengths = findMaxLengths();

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < table.get(0).size(); i++) {
      for (int j = 0; j < table.size(); j++) {
        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<Integer> findMaxLengths() {
    List<Integer> maxLengths = new ArrayList<>();
    for (List<String> row : table) {
      int maxLength = 0;
      for (String value : row) {
        if (value.length() > maxLength) {
          maxLength = value.length();
        }
      }
      maxLengths.add(maxLength);
    }
    return maxLengths;
  }
}