If your console output looks like a sloppy grocery list then welcome. This short guide shows how to use Java printf and format specifiers to produce neat, readable tables that humans can scan without therapy. We keep the output aligned, the decimals respectful, and the code reusable.
Start like a civilized person and decide on column labels and character widths. Think about the longest expected name and the largest number of digits you will get. Reserve a little slack so your table does not wrap mid drama.
Match Java types to specifiers. Use %s for strings, %d for integers and %f for floating point numbers. Add a width to reserve space and add precision to control decimal places. A minus flag before the width left aligns text like %-20s, while a plain width right aligns numbers like %8.2f.
Make one format string and reuse it. This is the boring magic that keeps every row obedient to the same column rules.
String fmt = "%-15s %8.2f\n"
System.out.printf(fmt, name, price)
Run a loop and feed each row into the template. Consistency wins here. One format string prevents one row from trying to steal the spotlight.
for (Product p : products) {
System.out.printf(fmt, p.getName(), p.getPrice())
}
If you want exactly two decimals use %8.2f. If a text column should hug the left edge use %-20s. If numbers should line up add sufficient width and right alignment. These are small changes that make big visual improvements.
Follow these steps and your console tables will stop resembling modern art. Use width, precision and the minus flag together and you will have tables that are both readable and annoyingly professional. If you need help tuning a specific layout paste a sample and we will make it behave.
I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!
This is a dedicated watch page for a single video.