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 OverflowJava output formatting for Strings - Stack Overflow
System.out.format in Java - Stack Overflow
format - Java System.out.print formatting - Stack Overflow
Formatting system.out.print when your printing a mix of numerical data and string
Videos
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
If you want a minimum of 4 characters, for instance,
System.out.println(String.format("%4d", 5));
// Results in " 5", minimum of 4 characters
Since you're using formatters for the rest of it, just use DecimalFormat:
import java.text.DecimalFormat;
DecimalFormat xFormat = new DecimalFormat("000")
System.out.print(xFormat.format(x + 1) + " ");
Alternative you could do whole job in whole line using printf:
System.out.printf("%03d %s %s %s \n", x + 1, // the payment number
formatter.format(monthlyInterest), // round our interest rate
formatter.format(principleAmt),
formatter.format(remainderAmt));
Since you are using Java, printf is available from version 1.5
You may use it like this
System.out.printf("%03d ", x);
For Example:
System.out.printf("%03d ", 5);
System.out.printf("%03d ", 55);
System.out.printf("%03d ", 555);
Will Give You
005 055 555
as output
See: System.out.printf and Format String Syntax
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();
}
}
}
}