System.out.println(String.format("%-20s= %s" , "label", "content" ));
  • Where %s is a placeholder for you string.
  • The '-' makes the result left-justified.
  • 20 is the width of the first string

The output looks like this:

label               = content

As a reference I recommend Javadoc on formatter syntax

Answer from stacker on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › tutorial › java › data › numberformat.html
Formatting Numeric Print Output (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)
There are many converters, flags, and specifiers, which are documented in java.util.Formatter ... The %d specifies that the single variable is a decimal integer. The %n is a platform-independent newline character. The output is: ... The printf and format methods are overloaded. Each has a version with the following syntax: public PrintStream format(Locale l, String format, Object...
Discussions

Formatting system.out.print when your printing a mix of numerical data and string
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/javahelp
6
3
September 7, 2021
How to use String.format() in Java? - Stack Overflow
So what I'm asking is if someone can explain to me how String.format() works and how the formatting above works. Thanks for the help! ... That method uses the Formatter syntax described in the Formatter javadocs. ... Post it as an answer @regettman, I was about to but you already put it here so now I feel like I'd be poaching if I did :P ... It works same as printf... More on stackoverflow.com
🌐 stackoverflow.com
Can someone explain this printf to me?
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full - best also formatted as code block You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/learnjava
10
9
September 17, 2022
[Java] Using String.format to format a string printed out of my own toString method, I am not able to right align the text though.
Format specifiers can contain an optional width field. For example, %20s will set the minimum width of the String to 20 characters (right-aligned). You can find the documentation here for more info. More on reddit.com
🌐 r/learnprogramming
2
9
April 2, 2014
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › How-to-format-a-Java-String-with-printf-example
How to format a Java String with printf example
Use %S as the printf String specifier to output upper-case text. Precede the letter s with a number to specify field width. Put a negative sign after the % to left-justify the text.
🌐
W3Schools
w3schools.com › java › ref_string_format.asp
Java String format() Method
Java Examples Java Videos Java ... = "Hello %s! One kilobyte is %,d bytes."; String result = String.format(myStr, "World", 1024); System.out.println(result);...
🌐
Baeldung
baeldung.com › home › java › core java › formatting output with printf() in java
Formatting Output with printf() in Java | Baeldung
January 8, 2024 - To break the string into separate lines, we have a %n specifier: ... The %n separator printf() will automatically insert the host system’s native line separator. To format Boolean values, we use the %b format.
🌐
Kansas State University
textbooks.cs.ksu.edu › cc210 › 09-strings › 06-java › 04-formatting
String Formatting :: CC 210 Textbook
June 27, 2024 - Java also includes a special string method, the format() method, which allows us to use placeholders in our output string, and then replace those placeholders with the values stored in variables. Here’s an example of how to use that method in Java: int sum = 123; double avg = 1.23; String name = "Student"; String output = "%s: Your score is %d with an average of %f."; System.out.println(String.format(output, name, sum, avg));
🌐
GeeksforGeeks
geeksforgeeks.org › java › formatted-output-in-java
Formatted Output in Java using printf() - GeeksforGeeks
August 16, 2024 - Char Formatting is easy to understand as it need printf() and Charracter format specifier used are '%c' and '%C'. ... // Java Program to Formatt // import java.io.*; // Driver Class class GFG { // main function public static void main(String[] ...
Find elsewhere
🌐
Home and Learn
homeandlearn.co.uk › java › java_formatted_strings.html
Java For Complete Beginners - formatted strings
Strings of text can be formatted and output with the printf command. The printf command understands a series of characters known as a format specification. It then takes a string of text and formats it, based on the format specification passed over.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Formatter.html
Formatter (Java Platform SE 8 )
October 20, 2025 - Java™ Platform Standard Ed. 8 ... An interpreter for printf-style format strings. This class provides support for layout justification and alignment, common formats for numeric, string, and date/time data, and locale-specific output. Common Java types such as byte, BigDecimal, and Calendar are supported.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-string-format-method-with-examples
Java String format() Method - GeeksforGeeks
July 11, 2025 - ... // Java program to demonstrate ... // Format the price with thousands // separator and two decimal places String s = String.format("%1$,10.2f", d); System.out.println("Formatted Price: " + s); } }...
🌐
Reddit
reddit.com › r/javahelp › formatting system.out.print when your printing a mix of numerical data and string
r/javahelp on Reddit: Formatting system.out.print when your printing a mix of numerical data and string
September 7, 2021 -

Hi there, I'm currently taking an intro to java class and I need to make a Temperature Conversion chart, formatted in such a way that the output needs to look like this and the decimal temperatures should be displayed using 10 spaces per temperature, with three digits shown after the decimal point. And The program should use static methods to perform the actual conversion, so that you can print the input temperatures and converted temperatures using a single System.out statement.

           Temperature Conversion Tables

       Temperature      |       Temperature
        (degrees)       |        (degrees)
      F           C     |      C           F
   −40.000     −40.000  |    −40.000     −40.000
   −35.000     −37.222  |    −35.000     −31.000
   −30.000     −34.444  |    −30.000     −22.000
ect.....

I'm having issues with applying the formatting just to the numerical data, I am unsure how to specify/separate the outprintf to only apply to the numerical data and not the other string information , such as "\t | \t" used to create the chart appears required, and therefore am getting this error

Exception in thread "main" java.util.IllegalFormatConversionException: f != java.lang.String

at java.base/java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4426)

at java.base/java.util.Formatter$FormatSpecifier.printFloat(Formatter.java:2951)

at java.base/java.util.Formatter$FormatSpecifier.print(Formatter.java:2898)

at java.base/java.util.Formatter.format(Formatter.java:2673)

at java.base/java.io.PrintStream.format(PrintStream.java:1053)

at java.base/java.io.PrintStream.printf(PrintStream.java:949)

at conversionTable.main(conversionTable.java:17)

Here is my complete source code so far

public class conversionTable { 

    public static void main(String[] args) { 
    
        System.out.println();
        System.out.println("\t    Temperature Conversion Table");
	System.out.println();
	System.out.println("\t Temperature \t | \t Temperature");
	System.out.println("\t  (degrees) \t | \t  (degrees)");
        System.out.println("\t F \t C \t | \t C \t F");

        final double TOTAL = 455.000;

        for (double c = -40.000; c <= TOTAL; c+=5)
	for (double f = -40.000; f <= TOTAL; f+=5)
				
        {
            System.out.printf(f + "\t" + 
				"%10.3f\n", (f-32)*5/9 + 
				"\t | \t" + c + "\t" + 
				"%10.3f\n", (c*9/5)+32);
            {
                System.out.println();
            }
        }
     }
}

Top answer
1 of 2
1
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
2 of 2
1
Your format string for printf is completely wrong. Please, familiarize yourself with formatter syntax: https://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html https://docs.oracle.com/javase/tutorial/java/data/numberformat.html https://www.geeksforgeeks.org/java-string-format-method-with-examples/ (String.format uses the exact same syntax) https://www.baeldung.com/java-string-formatter In short: you only need a single string, not a concatenated one and the parameters follow the string separated by commas. Also, use the printf method for the table headers. Only that way, you can achieve consistent alignment.
🌐
DZone
dzone.com › coding › java › formatting strings in java: string.format() method
Formatting Strings in Java: String.format() Method
November 13, 2024 - Here is an example of how to use String.format() in Java: ... class StringFormatExample { public static void main(String[] args) { String name = "Rob Gravelle"; String str = String.format("My name is %s", name); System.out.println(str); // My ...
🌐
Coderanch
coderanch.com › t › 623115 › java › java-String-format
Using java String.format() [Solved] (Beginning Java forum at Coderanch)
November 4, 2013 - Can | <-- This is 11 characters so it ends there but for plastic bag which evidently is 11 characters on its own this happens (it's 11 without some of the white space cleated out after posting) Plastic Bag| <-- They end at different points and so whatever needs to be formatted next (the values) come out jumbled up. I've also trimmed every string I can but nothing hcanges ... Go through each of those Strings and print its length on screen.
🌐
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 } }
🌐
Belief Driven Design
belief-driven-design.com › formatting-strings-with-java-42648c25697
Formatting Strings With Java | belief driven design
April 14, 2020 - Now we know about the different ways to format Strings. Which one to choose depends on our requirements. java.text.MessageFormat is easier on the eyes, especially if you’re not fluent in printf. But String.format is widely used and understood, so we can’t go wrong with it.
🌐
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 - Finally, all format specifiers, escape characters, etc. are also valid for the Formatter class as this is the main logic for formatting Strings in all three cases: String.format(), printf(), and Formatter. Finally, let's show one final formatting technique that doesn't use Formatter under the hood. MessageFormat was made to produce and provide concatenated messages in a language-neutral way. This means that the formatting will be the same, regardless of whether you're using Java, Python, or some other language that supports MessageFormat.
🌐
Medium
medium.com › @AlexanderObregon › javas-string-format-method-explained-82707214c953
Java’s String.format() Method Explained | Medium
August 20, 2024 - char letter = 'A'; String formatted = String.format("The first letter is %c", letter); System.out.println(formatted); // Output: The first letter is A · %b: This specifier is used for formatting boolean values (true or false). It converts a boolean into a string. boolean isJavaFun = true; String formatted = String.format("Is Java fun?