Wednesday, September 28, 2011

Building a tidy string

When we use the console for our output, we can carefully format the output using the System.out.printf method. We can format the strings for a JOptionPane dialog box, too, but there are a few extra steps involved. This example shows one way to build a formatted string using a Formatter from the java.util package.

Sample Code

Click on the screenshot to enlarge the image for easier reading.

Sample Output
Here is a screenshot of my code running.

StringBuilderExample version 2 output