The following code snippet will teach you how to align string in left, right or center alignment when you want to print out string to a console. We will print the string using the printf(String format, Object... args)
method. The format
specifier / parameter defines how the string will be formatted for output and the args
is the value that will be formatted.
The format
parameter / specifier include flags, width, precision and conversion-characters in the order shown below. The square brackets in the notation means the part is an optional parameter.
% [flags] [width] [.precision] conversion-character
Flags | Description |
---|---|
- |
left-align the output, when not specified the default is to right-align |
+ |
print (+ ) or (- ) sign for numeric value |
0 |
zero padded a numeric value |
, |
comma grouping separator for number greater that 1000 |
|
space will output a (- ) symbol for negative value and a space if positive |
Conversion | Description |
---|---|
s |
string, use capital S to uppercase the strings |
c |
character, use capital C to uppercase the characters |
d |
integer: byte , short , integer , long |
f |
floating point number: float , double |
n |
new line |
Width: Defines the field width for printing out the value of argument. It also represents the minimum number of characters to
be printed out to the output.
Precision: For floating-point conversion the precision define the number of digits of precision in a floating point value. For string value this will extract the substring.
To center the string for output we use the StringUtils.center()
method from the Apache Commons Lang library. This method will center-align the string str
in a larger string of size
using the default space character (‘ ‘). You can supply the third parameter to define your own space character / string.
package org.kodejava.lang;
import org.apache.commons.lang3.StringUtils;
import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.ChronoUnit;
public class StringAlignment {
private static final Object[][] people = {
{"Alice", LocalDate.of(2000, Month.JANUARY, 1)},
{"Bob", LocalDate.of(1989, Month.DECEMBER, 15)},
{"Carol", LocalDate.of(1992, Month.JULY, 24)},
{"Ted", LocalDate.of(2006, Month.MARCH, 13)},
};
public static void main(String[] args) {
String nameFormat = "| %1$-20s | ";
String dateFormat = " %2$tb %2$td, %2$tY | ";
String ageFormat = " %3$3s |%n";
String format = nameFormat.concat(dateFormat).concat(ageFormat);
String line = new String(new char[48]).replace('\0', '-');
System.out.println(line);
System.out.printf("|%s|%s|%s|%n",
StringUtils.center("Name", 22),
StringUtils.center("Birth Date", 16),
StringUtils.center("Age", 6));
System.out.println(line);
for (Object[] data : people) {
System.out.printf(format,
data[0], data[1],
ChronoUnit.YEARS.between((LocalDate) data[1], LocalDate.now()));
}
System.out.println(line);
}
}
Here is the output of our code snippet above:
------------------------------------------------
| Name | Birth Date | Age |
------------------------------------------------
| Alice | Jan 01, 2000 | 17 |
| Bob | Dec 15, 1989 | 27 |
| Carol | Jul 24, 1992 | 24 |
| Ted | Mar 13, 2006 | 10 |
------------------------------------------------
Maven Dependencies
<!-- https://search.maven.org/remotecontent?filepath=org/apache/commons/commons-lang3/3.12.0/commons-lang3-3.12.0.jar -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>