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 OverflowSystem.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
If you want a minimum of 4 characters, for instance,
System.out.println(String.format("%4d", 5));
// Results in " 5", minimum of 4 characters
Formatting system.out.print when your printing a mix of numerical data and string
How to use String.format() in Java? - Stack Overflow
Can someone explain this printf to me?
[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.
Videos
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();
}
}
}
}
It works same as printf() of C.
%s for String
%d for int
%f for float
ahead
String.format("%02d", 8)
OUTPUT: 08
String.format("%02d", 10)
OUTPUT: 10
String.format("%04d", 10)
OUTPUT: 0010
so basically, it will pad number of 0's ahead of the expression, variable or primitive type given as the second argument, the 0's will be padded in such a way that all digits satisfies the first argument of format method of String API.
It just takes the variables hour, minute, and second and bring it the the format 05:23:42. Maybe another example would be this:
String s = String.format("Hello %s answered your question", "placeofm");
When you print the string to show it in the console it would look like this
System.out.println(s);
Hello placeofm answered your question
The placeholder %02d is for a decimal number with two digits. %s is for a String like my name in the sample above. If you want to learn Java you have to read the docs. Here it is for the String class. You can find the format method with a nice explanation. To read this docs is really important not even in Java.
System.out.printf( "%-15s%03d %n", s1, x);
I know what the -15 means but is confusing me.