datalogo
Return to Unit Menu |  JavaBitsNotebook.com | MathBits.com | Terms of Use  | JavaMathBits.com

Formatting Output

Formatting options are of particular benefit when creating charts or tables of information. There are a variety of ways to format data in Java.

The format() method can be used instead of print() or println() to control the way your display will look. A format() provides a "template" of how the spacing of your information will occur. This template is called the format string specifier, and takes the form:

% [alignment] [width] s  

% indicates that the specifier (template) is starting, for each section
[alignment] not needed for right alignment. Include (-) for left alignment.
[width] is the number of characters to use for output
s indicates that the corresponding argument is a string

format() behaves similar to print() in that it will leave you on the same line, and will not move down to the next line. You will need to force a "\n" at the end of each line to move to the next line.

If [width] is larger than the number of characters in the corresponding string, then extra spaces will pad the output. If [width] is smaller than the number of characters in the corresponding string, will be displayed and any strings to the right will be moved over.


Example 1:
format1
Display:
format1D

dividerdash

Example 2:

You can also mix the printing of " normal text" within the format string.

format3
Display:
format3b

dividerdash

It is also possible to use the format() method to format numeric values. For floating point numbers a specifier can take the form:

% [alignment] [width] [.decimal] f  

% indicates that the specifier (template) is starting, for each section
[alignment] not needed for right alignment. Include (-) for left alignment.
[width] is the number of characters to use for output
f indicates that the corresponding argument is a floating point number (or double)

For integer values a specifier can take the form:

% [alignment] [width] d 

% indicates that the specifier (template) is starting, for each section
[alignment] not needed for right alignment. Include (-) for left alignment.
[width] is the number of characters to use for output
d indicates that the corresponding argument is an integer value
Be careful! "d" stands for "integer", not "double".

If numeric values exceed the formatting number of decimal digits, normal mathematical rounding will occur.

Example 3: (floating point, doubles)
format6

Display:
format6a
Notice how the %15.2f prints 2 decimal places (rounded if needed).

dividerdash

Example 4: (integers)
format4

Display:
      3       4

divider
Return to Unit Menu |  JavaBitsNotebook.com | MathBits.com | Terms of Use  | JavaMathBits.com

Notice: These materials are free for your on-line use at this site, but are not free for the taking.
Please do not copy these materials or re-post them to the Internet, as it is copyright infringement.
If you wish hard-copies of these materials, refer to our subscription area, JavaMathBits.com.
Help us keep these resources free. Thank you.