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 › essential › io › formatting.html
Formatting (The Java™ Tutorials > Essential Java Classes > Basic I/O)
Other flags include - (pad on the right) and , (format number with locale-specific thousands separators). Note that some flags cannot be used with certain other flags or with certain conversions. The Argument Index allows you to explicitly match a designated argument. You can also specify < to match the same argument as the previous specifier. Thus the example could have said: System.out.format("%f, %<+020.10f %n", Math.PI);
🌐
Alvin Alexander
alvinalexander.com › blog › post › java › java-string-formatted-format-output-text
Java ‘printf’ - formatting output with System.out.format | alvinalexander.com
July 28, 2022 - For instance, here’s the usual “Hello, world” example, using these new Java formatting and printing methods: ... While that example hardly makes it look very valuable, here’s a better Java printf-style formatting example that shows the power of these formatting methods: System.out.format("The '%s' method died at line %d at '%s'.", methodName, lineNumber, currentTime);
Discussions

Java output formatting for Strings - Stack Overflow
I was wondering if someone can show me how to use the format method for Java Strings. For instance If I want the width of all my output to be the same More on stackoverflow.com
🌐 stackoverflow.com
System.out.format in Java - Stack Overflow
I'm trying to make a formatted output in Java but I'm having some trouble... I write for example: System.out.format ("%d", 5); but Eclipse underlines the word "format" and there is a marker at thi... More on stackoverflow.com
🌐 stackoverflow.com
format - Java System.out.print formatting - Stack Overflow
Here is my code (well, some of it). The question I have is, can I get the first 9 numbers to show with a leading 00 and numbers 10 - 99 with a leading 0. I have to show all of the 360 monthly paym... More on stackoverflow.com
🌐 stackoverflow.com
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
January 6, 2022
🌐
TutorialsPoint
tutorialspoint.com › Formatted-Output-in-Java
Formatted Output in Java
December 26, 2024 - In Java, we have a format() method to retrieve the formatted output. This method belongs to the string class, which prints formatted data. In the following example, we will format and display the formatted output of the given data using the System.out.format() method.
🌐
YouTube
youtube.com › logic lambda
Using printf & System.out.format in Java - YouTube
This video walks through generating output from a Java program using System.out.printf & System.out.format0:06 - Overview of printf & format (format string, ...
Published   May 22, 2023
Views   764
🌐
Baeldung
baeldung.com › home › java › core java › formatting output with printf() in java
Formatting Output with printf() in Java | Baeldung
January 8, 2024 - Definitely, for more advanced formatting, we can use DateTimeFormatter, which has been available since Java 8. First, let’s see the list of some useful suffix characters for time formatting: H, M, S characters are responsible for extracting the hours, minutes and seconds from the input Date. L, N represent the time in milliseconds and nanoseconds accordingly. ... However, listing date multiple times is a pain. Alternatively, to get rid of multiple arguments, we can use the index reference of our input parameter, which is 1$ in our case: System.out.printf("%1$tH:%1$tM:%1$tS %1$tp %1$tL %1$tN %1$tz %n", date);
Find elsewhere
🌐
Oracle
docs.oracle.com › javase › tutorial › java › data › numberformat.html
Formatting Numeric Print Output (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)
These methods, format and printf, ... object, so you can invoke PrintStream methods on System.out. Thus, you can use format or printf anywhere in your code where you have previously been using print or println....
🌐
Study.com
study.com › computer science courses › computer science 109: introduction to programming
Using the printf Method for Standard Output in Java | Study.com
Like other programming languages, in Java you can use the printf method using System.out.printf for formatting the output of numbers and strings. The method takes the format string as the first parameter followed by a list ...
🌐
W3Schools
w3schools.com › java › ref_output_printf.asp
Java Output printf() Method
If the character is uppercase the data will be formatted in uppercase where possible. The list of possible characters is shown in the table below. ... // Default System.out.printf("%f%n", 123456.78); // Two decimal digits System.out.printf("%.2f%n", 123456.78); // No decimal digits System.out.printf("%.0f%n", 123456.78); // No decimal digits but keep the decimal point System.out.printf("%#.0f%n", 123456.78); // Group digits System.out.printf("%,.2f%n", 123456.78); // Scientific notation with two digits of precision System.out.printf("%.2e", 123456.78);
🌐
GeeksforGeeks
geeksforgeeks.org › java › formatted-output-in-java
Formatted Output in Java using printf() - GeeksforGeeks
August 16, 2024 - Most users are familiar with the printf function in C. Let us discuss how we can Formatting Output with printf() in Java in this article. printf() uses format specifiers for formatting. There are certain data types are mentioned below: ... The number itself includes Integer, Long, etc. The formatting Specifier used is %d. ... // Java Program to demonstrate // Use of printf to // Formatting Integer import java.io.*; // Driver Class class GFG { // main function public static void main (String[] args) { int a=10000; //System.out.printf("%,d%n",a); System.out.printf("%,d%n",a); } }
🌐
Sdsc
users.sdsc.edu › ~phil › cse11 › lectures › lecture14 › FormatMe.java
https://users.sdsc.edu/~phil/cse11/lectures/lectur...
// Some samples of format strings // Compile: javac FormatMe.java // run: java FormatMe [N] // N number of squares to calculate in a table public class FormatMe { public static void main(String[] args) { int MAX = 12; if (args.length >= 1) MAX=Integer.parseInt(args[0]); // print 10-19 in 5-wide columns in decimal and hex for (int i = 10; i < 20; i++) System.out.format("]", i); // 5 spaces wide ints System.out.format("\n"); for (int i = 10; i < 20; i++) System.out.format("%5x", i); // 5 spaces wide hexadecimal System.out.format("\n"); // print some examples of doubles System.out.format("PI with no format modifiers (%%f) %f\n", Math.PI); System.out.format("PI with in exponent form (%%8.3e) %8.3e\n", Math.PI); System.out.format("PI extended width form (% .16f) .16f\n", Math.PI); System.out.format("PI with in exponent form (% .16e) .16e\n", Math.PI); // "print" to a String.
🌐
Sololearn
sololearn.com › en › Discuss › 2070043 › what-is-systemoutformat
What is system.out.format()? | Sololearn: Learn to code for FREE!
the difference is with format you can use place holders(format specifiers %d %s ... ) for variable inside strings without concatenation. with print you have to concatenate string inside of the print(). ex: string username = "M. J." ; ...
🌐
Medium
medium.com › @kalpana.p_65621 › system-out-printf-in-java-a23690d66f97
System.out.printf() in JAVA. In Java 5.0 and higher versions, the… | by Kalpana P | Medium
January 30, 2025 - Below are some of the commonly used format specifiers: %d — represents a decimal integer %f — Floating-point number (applicable to both float and double) %s — String %c — Character %x — Hexadecimal integer %o — Octal integer %b — Binary integer etc., NOTE: In Java’s printf() method, %n is a format specifier representing a platform-independent newline character and is similar to \n in C and C++. ... Similar to printf() statement in C, we can use System.out.printf() for the formatted output in JAVA and it is applicable from Java 5.0.
🌐
Jmu
wiki.cs.jmu.edu › reference › java › formatting
Formatting Strings and Output in Java - JMU CS Wiki
int year = 2017; double sales = 1050987.00; String s; s = String.format("Year: M, Sales: $%,14.2f", year, sales); System.out.println(s);
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-printf-method
Java printf() - Print Formatted String to Console | DigitalOcean
August 3, 2022 - Instead, we can replace them with a single one: jshell> System.out.printf("%1$tH:%1$tM:%1$tS %1$Tp GMT %1$tz %n", date) 02:15:36 AM GMT +0530 · Date formatting has the following special characters A/a - Full day/Abbreviated day B/b - Full month/Abbreviated month d - formats a two-digit day of the month m - formats a two-digit month Y - Full year/Last two digits of the Year j - Day of the year
🌐
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
January 6, 2022 -

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.
🌐
ziddigyan
ziddigyan.wordpress.com › 2017 › 02 › 25 › java-formatting-strings
Java – Formatting System.out.format() - ziddigyan
February 25, 2017 - ================================ java 100 cpp 065 python 050 ================================ Explanation · Each String is left-justified with trailing whitespace through the first 15 characters. The leading digit of the integer is the 16th character, and each integer that was less than 3 digits now has leading zeroes. First of all we will use System.out.format instead of System.out.println as it is use to format the output.
🌐
HackerRank
hackerrank.com › challenges › java-output-formatting › forum
Java Output Formatting Discussions | Java | HackerRank
public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("================================"); for(int i=0;i<3;i++){ String s1=sc.next(); int x=sc.nextInt(); StringBuilder s = new StringBuilder(); for(int j=0;j<15-s1.length();j++){ s.append(" "); } System.out.println(s1+s+String.format("d",x)); } System.out.println("================================"); sc.close(); } }