Since you already have guava on the classpath.. I am going to show you what I came up with, but this is probably over-simplified and needs some more work to be done, even though not too much.
You need to always know the maximum size of the String in one row. I am calculating here only one actually:
private static final class SortBylenght extends Ordering<String>{
@Override
public int compare(String left, String right) {
return Ints.compare(left.length(), right.length());
}
}
Then calculate the max:
ImmutableList<String> data = ImmutableList.of(firstName, school);
SortBylenght sortBylenght = new SortBylenght();
String maxString = sortBylenght.max(data);
int maxSize = maxString.length();
Then print:
bw.write(Strings.padEnd(firstName, maxSize, ' '));
for(int i=0;i<20;++i) bw.write(' ');
//You need to pad the end here of course again like in the first row;
bw.write(secondName);
bw.newLine();
bw.write(Strings.padEnd(school, maxSize, ' '));
for(int i=0;i<20;++i) bw.write(' ');
//You need to pad the end here of course again like in the first row;
bw.write(yob);