printf in Java?
I have several data object with different length and I want to print them nicely in columns. In C++ and C, printf can handle different formatting easily with the formatting char. I'm using a java .library by Horstmann that implements printf.
Since I'm not up-to-date with the latest Java, I'm curious if it has any native utility that handles pretty printing. Any pointer will be appreciated.
Thanks
# 2
You can use PrintWriter.printf. Java 5 have IMHO great formating capabilities. Look at
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html#syntax
E.g. print row 5 space right aligned integer, 20 space left aligned stringSystem.out.printf("|%5d|%-20s|\n", id, name);
Pipes are just table borders, no formating characters.
# 4
Or java.util.Formmater if you want to write to: File, OutputStream, Writer, StringBuffer, StringBuilderBut Kajs point on Sting.format was a good one. With varargs format's finally an usefull method. O:-)