Last active
July 12, 2016 18:54
-
-
Save XaveScor/3d6e441ce077b4f40b2f7b9257086646 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package kz.xavescor; | |
import java.util.*; | |
public class Main { | |
public static void main(String[] args) { | |
Scanner in = new Scanner(System.in); | |
ArrayList<ArrayList<String>> table = new ArrayList<>(); | |
while (in.hasNextLine()) { | |
ArrayList<String> row = new ArrayList<>(); | |
row.addAll(Arrays.asList(in.nextLine().split("\\|"))); | |
for (int i = 0; i < row.size(); ++i) { | |
row.set(i, row.get(i).trim()); | |
} | |
table.add(row); | |
} | |
ArrayList<Integer> maxLen = new ArrayList<>(table.get(0).size()); | |
for (int i = 0; i < table.get(0).size(); ++i) { | |
maxLen.add(0); | |
} | |
for (ArrayList<String> str : table) { | |
for (int col = 1; col < str.size(); ++col) { | |
maxLen.set(col, Math.max( | |
maxLen.get(col), str.get(col).length() | |
)); | |
} | |
} | |
for (ArrayList<String> str : table) { | |
System.out.print('|'); | |
for (int col = 1; col < str.size(); ++col) { //because first element is empty | |
System.out.print(normalize(str.get(col), maxLen.get(col)) + '|'); | |
} | |
System.out.println(); | |
} | |
} | |
private static String normalize(String s, Integer integer) { | |
int spacesNeeded = (integer - s.length()) / 2; | |
String before = new String(new char[spacesNeeded]).replace("\0", " "); | |
String after = before; | |
if (spacesNeeded % 2 == 1) { | |
after += " "; | |
} | |
return before + s + after; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment