How to Center Text Output in Java
Last updated: October 28, 2023
1. Introduction
In a Java-based text application, it is often necessary to center on text. Additionally, by centering the text while developing an application for Xbox, a command line tool, or any other application that involves improving the visual representation of text while user inputs, the user is likely to have a good experience in using that program.
In this tutorial, we’ll dive into different methods to center-align text output in Java.
2. Using String Formatting
The fastest way to center text in Java can be provided by using the String class’s format() method. Let’s see the following example:
@Test
public void givenTextAndTotalWidth_whenUsingStringFormat_thenTextIsCentered() {
String text = "Centered Text";
int totalWidth = 15;
int padding = (totalWidth - text.length()) / 2;
String centeredText = String.format("%" + padding + "s%s%" + padding + "s", "", text, "");
Assert.assertEquals(" Centered Text ", centeredText);
}
In this test method, we’d like to display “Centered Text” within the total width of 20 characters. Hence, we calculate how many spacings are needed for both sides. Then, we fill up the required number of strings that have a length equal to the number of spacings by using a method called String.format() to set the right amount of characters.
3. Using StringBuilder
Another way to center on building text is through StringBuilder. It enables more alignment tweaking, hence being more flexible. Let’s take the following example:
@Test
public void givenTextAndTotalWidth_whenCenterUsingStringBuilder_thenTextIsCentered() {
String text = "Centered Text";
int width = 15;
int padding = (width - text.length()) / 2;
StringBuilder centeredText = new StringBuilder();
for (int i = 0; i < padding; i++) {
centeredText.append(" ");
}
centeredText.append(text);
for (int i = 0; i < padding; i++) {
centeredText.append(" ");
}
String centeredTextString = centeredText.toString();
assertEquals(" Centered Text ", centeredTextString);
}
In the above test method, we create a StringBuilder where we pad it with spaces to add the required padding space and append our main content. Then, pad it again to add the rest of the padding spaces before printing the centered text.
4. Using StringUtils
One more easy way to center text is utilizing the center() method of the StringUtils class of Apache Commons Lang that was created especially for such purpose. Let’s practice the following example:
@Test
public void givenTextAndTotalWidth_whenUsingStringUtilsCenterMethod_thenTextIsCentered() {
String text = "Centered Text";
int width = 15;
String centeredText = StringUtils.center(text, width);
assertEquals(" Centered Text ", centeredText);
}
In the above test method, we use the center() method of the StringUtils class that takes the text string and its total width. It then centers the text within the specified width.
5. Conclusion
In conclusion, we can say that there are multiple ways to center text output in Java, such as String formatting, StringBuilder, and StringUtils from the Apache Commons Lang libraries.
Furthermore, these methods enhance the aesthetics of Java text-based applications, and users find it easy to interact with them.
As always, the complete code samples for this article can be found over on GitHub.