Introduction
In this lab, you will be using the printf() method in Java. The printf() method is used to format and print a string to the console. The general syntax of the method is:
In this lab, you will be using the printf() method in Java. The printf() method is used to format and print a string to the console. The general syntax of the method is:
Open the terminal and create a project directory using the command:
mkdir project
Create a new file inside the project directory using the command:
touch project_formatting_printf.java
To format integers using printf() we use the %d
format specifier. Below is an example:
public static void main(String[] args) {
int num = 10;
System.out.printf("The number is: %d%n", num);
}
In the above code, %d
is the format specifier for integers and %n
is the newline character.
Compile and run the code using the following commands:
javac project_formatting_printf.java
java project_formatting_printf
You will see the output:
The number is: 10
To format decimals, use the %f
format specifier. For example:
public static void main(String[] args) {
double num = 3.14159265359;
System.out.printf("The number is: %.2f%n", num);
}
In the above code, %.2f
is the format specifier for decimals with 2 decimal places.
Compile and run the code using the following commands:
javac project_formatting_printf.java
java project_formatting_printf
You will see the output:
The number is: 3.14
To format dates, we use t as the conversion character and give a format specifier. For example:
public static void main(String[] args) {
Date date = new Date();
System.out.printf("Today is %tA, %<tB %<te, %<tY.%n", date);
}
In the above code, %tA
is the format specifier for the full name of the day and %<tB %<te, %<tY
is the format specifier for the month, day and year. %n
is the newline character.
Compile and run the code using the following commands:
javac project_formatting_printf.java
java project_formatting_printf
You will see the output:
Today is Friday, October 15, 2021.
Flags are used to add special features to the output. For example:
public static void main(String[] args) {
double num = 12345.6789;
System.out.printf("The number is: %,.2f%n", num);
}
In the above code, ,
is the thousands separator and %.2f
is the format specifier for decimals with 2 decimal places.
Compile and run the code using the following commands:
javac project_formatting_printf.java
java project_formatting_printf
You will see the output:
The number is: 12,345.68
Precision is used to limit the number of decimal places. For example:
public static void main(String[] args) {
double num = 1234.56789;
System.out.printf("The number is: %.2f%n", num);
}
In the above code, %.2f
is the format specifier for decimals with 2 decimal places.
Compile and run the code using the following commands:
javac project_formatting_printf.java
java project_formatting_printf
You will see the output:
The number is: 1234.57
To print the output in uppercase, we use the uppercase of the conversion character. For example:
public static void main(String[] args) {
String name = "John Doe";
System.out.printf("Name: %S%n", name);
}
In the above code, %S
is the format specifier for uppercase strings.
Compile and run the code using the following commands:
javac project_formatting_printf.java
java project_formatting_printf
You will see the output:
Name: JOHN DOE
We can use padding to add spaces or zeroes to the output. For example:
public static void main(String[] args) {
int num = 123;
System.out.printf("The number is: %010d%n", num);
}
In the above code, %010d
is the format specifier for integers with 10 digits and the padding character is 0.
Compile and run the code using the following commands:
javac project_formatting_printf.java
java project_formatting_printf
You will see the output:
The number is: 0000000123
We can combine multiple format specifiers to format the output as we want. For example:
public static void main(String[] args) {
double num = 1234.56789;
System.out.printf("The number is: %,010.2f%n", num);
}
In the above code, %010.2f
is the format specifier for decimals with 2 decimal places, 10 digits, and the padding character is 0. ,
is the thousands separator.
Compile and run the code using the following commands:
javac project_formatting_printf.java
java project_formatting_printf
You will see the output:
The number is: 01,234.57
Congratulations! You have completed the Formatting With Printf lab. You can practice more labs in LabEx to improve your skills.