If you want to put the result in a String you can use :

String result = String.format("%-30s %-30s %-30s", divName, heading1, heading2);

Then you can print it or use it in another places

Answer from Youcef LAIDANI on Stack Overflow
🌐
Alvin Alexander
alvinalexander.com › blog › post › java › use-string-format-java-string-output
Java String formatting with the String.format method (like ‘sprintf’) | alvinalexander.com
July 30, 2024 - Here are some examples: // output ... RENAME_SUCCEEDED); // format a string with multiple variables (interpolation) log.debug( String.format("%s is %d years old, er, young", "Al", 45) );...
🌐
BeginnersBook
beginnersbook.com › 2017 › 10 › java-string-format-method
Java String format() method
June 9, 2024 - public class Example{ public static void main(String args[]){ String name = "Chaitanya"; int age = 37; double luckyNum = 123.456; String formattedString = String.format("Name: %s, Age: %d, Lucky No: $%.2f", name, age, luckyNum); System.out.println(formattedString); // Output: Name: Chaitanya, Age: 37, Lucky No: $123.456 } } import java.util.Date; public class Example{ public static void main(String args[]){ Date date = new Date(); String formattedDate = String.format("Current DateTime: %tc", date); System.out.println(formattedDate); // Output: Current DateTime: Sat Jun 08 07:39:52 PDT 2024 } }
Discussions

How to format multiple string parameters in Java - Stack Overflow
The problem I'm having is specifying the length of the strings divName, heading1, and heading2. "%-30s" formats one of them, on either end, but for the life of me, I can't get the formatting right to set a length for each one. I tried three sets of specifiers separated by commas but it just ... More on stackoverflow.com
🌐 stackoverflow.com
January 25, 2018
java - How to add multiple strings in a format to another string - Stack Overflow
String concatenation is a feature that has been planned for Java 11. Keep watching. ... Using the String.format style (similar to C's 'printf' syntax) allows you to see the structure of your final string more clearly even when variable names are long. More on stackoverflow.com
🌐 stackoverflow.com
java - Do I have to specify a variable for each identical argument in String.format? - Stack Overflow
String hello = "Hello"; String.format("%s %s %s %s %s %s", hello, hello, hello, hello, hello, hello); hello hello hello hello hello hello Does the hello variable need to be More on stackoverflow.com
🌐 stackoverflow.com
May 10, 2019
How to format and print multiple strings in Java - Stack Overflow
When outputting first and last, it combines them both. How would I go about making First and Last be outputted separately. I also tried add an output for the total number of characters would I decl... More on stackoverflow.com
🌐 stackoverflow.com
September 9, 2019
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-string-format-method-with-examples
Java String format() Method - GeeksforGeeks
June 2, 2026 - IllegalFormatException:If the format specified is illegal or there are insufficient arguments. Example: Using String.format() method to show concatinate the two floating values of given variable using /.
🌐
Javaprogramto
javaprogramto.com › 2019 › 03 › java-string-format.html
Java String format() Examples JavaProgramTo.com
November 13, 2021 - String formatted value: String methods on website Java-W3schools blog Float formatted string value: String methods on website 12.3450009 · Example Program to format the string with multiple string arguments.
🌐
Programiz
programiz.com › java-programming › library › string › format
Java String format()
n1 in octal: 57 n1 in hexadecimal: 2f n1 in hexadecimal: 2F n1 as string: 47 n2 as string: 35.864 n3 in scientific notation: 4.45343e+07 · You can use more than one format specifier in the format string.
Find elsewhere
🌐
Baeldung
baeldung.com › home › java › java string › java string.format()
Java String.format() | Baeldung
March 23, 2026 - A quick example and explanation of the format API of the standard String class in Java.
🌐
Stack Abuse
stackabuse.com › how-to-format-a-string-in-java-with-examples
Format String in Java with printf(), format(), Formatter and MessageFormat
October 22, 2021 - Or, you might have a predefined message for greeting users, but would like to format it to include the username. The arguments vararg conveniently expects the arguments (i.e. values) for the template String. For example, if the template has placeholders for two numbers, the printf() method will also be expecting two numbers as arguments:
🌐
DZone
dzone.com › data engineering › data › a clarified string formatting cheatsheet
A Clarified String Formatting Cheatsheet - DZone
September 22, 2017 - Good to keep close by, this simplified guide to String formatting in Java covers numbers, the available flags, and handling multiple arguments.
🌐
Dot Net Perls
dotnetperls.com › format-java
Java - String.format Examples - Dot Net Perls
June 1, 2023 - This example uses String.format with a Calendar date. We use "t" and then "B" to insert the long month string. With "t" and then "e" or "Y" we insert the day or year numbers. Note We use the "%1" at the start of the insertion points to indicate "the first argument" after the format string. import java.util.Calendar; public class Program { public static void main(String[] args) { // Create a calendar with a specific date.
🌐
Medium
medium.com › @AlexanderObregon › javas-string-format-method-explained-82707214c953
Java’s String.format() Method Explained | Medium
August 20, 2024 - %s: This specifier formats strings. It’s particularly useful when you want to insert text or combine multiple strings into one.
🌐
Vultr Docs
docs.vultr.com › java › standard-library › java › lang › String › format
Java String format() - Format String Text | Vultr Docs
December 20, 2024 - Use the String.format() method to replace placeholders with actual values. ... String name = "John"; String message = String.format("Hello, %s!", name); System.out.println(message); Explain Code · In this example, %s is a placeholder for a string.
🌐
Learnsic
learnsic.com › blog › how-to-format-strings-in-java
How to Format Strings in Java
April 4, 2024 - Java provides several ways to format strings, including concatenation, string literals, and the String.format() method. String concatenation is the process of combining two or more strings into a single string. In Java, you can use the + operator to concatenate strings. Here's an example:
🌐
Team Treehouse
teamtreehouse.com › community › java-basics-multiple-format-strings
java basics multiple format strings (Example) | Treehouse Community
September 25, 2015 - I tried what you were doing and was temporarily confused as well but what you have to do is pass in text inside the input field. So for example · String name = console.readLine("What is your name?");
🌐
W3Schools
w3schools.com › java › ref_string_format.asp
Java String format() Method
Strings Concatenation Numbers and Strings Special Characters Code Challenge Java Math Java Booleans · Booleans Real-Life Example Code Challenge Java If...Else
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › java string format
The Power of Java String Format: Harnessing the Full Potential of String Manipulation
March 3, 2025 - While String.format() is a powerful built-in method for formatting strings, Java provides alternative approaches for more efficient, localized, or complex string manipulation. These methods include StringBuilder, MessageFormat, and external libraries like Apache Commons. 1. Using StringBuilder and StringBuffer for Efficient Concatenation · Using + for string concatenation inside loops creates multiple ...
🌐
DZone
dzone.com › coding › java › formatting strings in java: string.format() method
Formatting Strings in Java: String.format() Method - DZone
November 13, 2024 - Core Java Specialization | Enroll in Free Online Course Today* ... There are three primary ways to format a string in Java. You can use the String.format() method, the printf() method, or the MessageFormat class for formatting strings.