Output formatting helps make program results clear and easy to read. It arranges text and numbers neatly on the screen.
Output formatting basics in Java
System.out.printf("format string", values); // or System.out.format("format string", values);
The format string uses special codes like %d for integers, %f for floating numbers, and %s for strings.
You can add width and precision to control spacing and decimal places, for example %.2f shows 2 decimals.
System.out.printf("Hello %s!", "Alice");
System.out.printf("Price: $%.2f", 12.5);
System.out.printf("Count: %5d", 42);
System.out.printf("%-10s is cool", "Java");
This program prints a name left-aligned in 10 spaces, an age right-aligned in 3 spaces, and a score with 2 decimals.
public class Main { public static void main(String[] args) { String name = "Bob"; int age = 30; double score = 95.6789; System.out.printf("Name: %-10s Age: %3d Score: %.2f\n", name, age, score); } }
Always match the format specifiers (%d, %f, %s) with the correct data type to avoid errors.
Use \n inside the format string to add a new line after printing.
Output formatting is useful for making your program's output look professional and easy to understand.
Use System.out.printf or System.out.format to format output in Java.
Format specifiers control how values appear, including alignment and decimal places.
Formatted output makes data easier to read and understand.
Practice
%d or %s?Solution
Step 1: Identify formatting methods in Java
System.out.printf and System.out.format are used for formatted output, unlike println or print which just print raw text.Step 2: Recognize the method that uses placeholders
System.out.printf uses format specifiers like %d for integers and %s for strings to format output.Final Answer:
System.out.printf -> Option BQuick Check:
Formatted output uses printf [OK]
- Confusing println with printf
- Using print which lacks formatting
- Thinking read formats output
num with two digits padded with zeros using printf?Solution
Step 1: Understand zero-padding format specifier
In printf, %02d means print an integer padded with zeros to width 2.Step 2: Check each option's correctness
%2d pads with spaces, %02d pads with zeros, %d2 and %d02 are invalid formats.Final Answer:
System.out.printf("%02d", num); -> Option AQuick Check:
Zero padding uses %0Nd [OK]
- Using %2d pads with spaces, not zeros
- Placing digits after %d incorrectly
- Confusing padding syntax
int x = 7;
System.out.printf("Value: %4d!", x);Solution
Step 1: Understand %4d formatting
%4d means print integer right-aligned in a field width of 4 characters, padding with spaces on the left.Step 2: Apply formatting to value 7
7 is one digit, so 3 spaces are added before it, resulting in " 7".Final Answer:
Value: 7! -> Option AQuick Check:
Right-align integer in 4 spaces [OK]
- Assuming padding is on the right
- Confusing zero padding with space padding
- Ignoring field width
double pi = 3.14159;
System.out.printf("%.2f", pi);
What is the error in this code?Solution
Step 1: Check format specifier for double
%.2f formats floating-point numbers with 2 decimal places, valid for double type.Step 2: Verify code correctness
Code prints 3.14 without error; newline is optional and casting is unnecessary.Final Answer:
No error; it prints 3.14 correctly. -> Option CQuick Check:
%.2f formats double with 2 decimals [OK]
- Thinking casting to float is needed
- Assuming newline is mandatory
- Believing %.2f is invalid
printf. Which format string correctly aligns the name left in 10 spaces and age right in 3 spaces?
String name = "Anna";
int age = 9;
System.out.printf("???", name, age);Solution
Step 1: Understand alignment flags in printf
%-10s means left-align string in 10 spaces; %3d means right-align integer in 3 spaces.Step 2: Match format to requirement
"%-10s %3d" matches left-align name and right-align age correctly; others swap or misalign.Final Answer:
"%-10s %3d" -> Option DQuick Check:
Left-align string with - flag, right-align number default [OK]
- Swapping alignment flags
- Using wrong width numbers
- Misplacing % signs
Ready to prove it?
Pass the test and this pattern moves unknown
