public class Divers {
public static void main(String args[]){
String format = \"|%1$-10s|%2$-10s|%3$-20s|\\n\";
System.out.format(format, \"FirstName\",
Here's the answer using apache commons lang StringUtils.
Please note that you have to add the jar file to the build path. If you are using maven make sure to add commons lang in the dependencies.
import org.apache.commons.lang.StringUtils;
public class Divers {
public static void main(String args[]){
String format = "|%1$-10s|%2$-10s|%3$-20s|\n";
System.out.format(format, "FirstName", "Init.", "LastName");
System.out.format(format,StringUtils.center("Real",10),StringUtils.center("",10),StringUtils.center("Gagnon",20);
System.out.format(String.format(format, (Object[])ex));
}
}