
Java Output Formatting asks you to print a small two-column table: a word on the left, padded to a fixed width, and a number on the right, always shown with three digits. There is no algorithm here. The whole problem is about one tool, System.out.printf, and its format specifiers like %-15s and %03d.
It is worth learning properly because you will need it again: printing money with two decimal places, lining up a report, zero-padding roll numbers or times like 09:05. People who only know string joining end up writing loops to add spaces by hand. With printf it is one short pattern.
Problem
There are three lines of input. Each line has a word and an integer. For each line, print one row with two columns:
- Column 1 is the word, left-aligned in a field exactly 15 characters wide (so shorter words are followed by spaces).
- Column 2 is the integer, printed as exactly three digits, with leading zeros if it has fewer than three digits.
The rows sit between two lines of 32 equals signs, which the starter code already prints.
Input format: three lines, each with a string and an integer separated by a space.
Output format: the top border, three formatted rows, the bottom border.
Constraints: each string has at most 10 letters; each integer is between 0 and 999.
Input:
java 100
cpp 65
python 50
Output:
================================
java 100
cpp 065
python 050
================================
Approach
Each output row is 18 characters: 15 for the word column and 3 for the number. For "cpp" that means the 3 letters, then 12 spaces, then 065. The number always starts at the 16th character.
You could compute the spaces yourself with a loop, but printf does it for you. You describe the shape of the line once:
%-15s: print a string in a field 15 wide, aligned to the left.%03d: print an integer in a field 3 wide, filling empty places with zeros.%n: end the line.
Put them together as "%-15s%03d%n", with no space between the two parts, because the padding already provides the gap.
Dry run for cpp 65: %-15s prints cpp plus 12 spaces to reach width 15. %03d sees 65, which has two digits, so it adds one leading zero and prints 065. The row is cpp 065.
Solution in Java
import java.util.Scanner;
public class Solution {
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();
// %-15s : string, left-justified in a 15-character column
// %03d : integer, at least 3 wide, padded with leading zeros
System.out.printf("%-15s%03d%n", s1, x);
}
System.out.println("================================");
sc.close();
}
}
Breaking down the format specifiers, since they are the whole point of this problem:
%smeans "insert a string here".%dmeans "insert an integer here".- A number after
%is the minimum width.%15sreserves 15 characters. If the value is shorter, Java pads it with spaces. If it is longer, Java prints it in full and does not cut it. -(the minus flag) means left-align. Without it,%15swould put the spaces before the word, which is right-alignment.0(the zero flag) on a number means pad with zeros instead of spaces.%3dwould print65, while%03dprints065.sc.next()reads one word, which is right here because the strings have no spaces.sc.nextInt()then reads the number on the same line.
The solution that was posted here earlier stopped at the starter code and never printed the rows; the loop body only had a "Complete this line" comment. The printf line above is the missing piece. Also note the old sample output on this page lost its spaces, so it looked like cpp 065. The real expected output has the padding shown above.
I tested the sample and an edge case with a 10-letter word and the extreme numbers: abcdefghij 0, x 5, y 999. They printed as abcdefghij 000, x 005 and y 999, each 18 characters wide.
Output
================================
java 100
cpp 065
python 050
================================
Complexity
Three rows of fixed width, so O(1) time and O(1) space.
Common mistakes
- Writing
%15swithout the minus. The words get right-aligned and every row is wrong. - Adding a space between the specifiers, like
"%-15s %03d". That makes the number start at character 17 instead of 16. - Forgetting
%nor\n, which joins all three rows into one line. - Using
%3dinstead of%03d, which pads with spaces, not zeros.
Before this: Java Stdin and Stdout II. Next: Java Loops I, which also uses printf.
Tip: try to solve the problem yourself before reading the solution. Use the code as a reference to check your approach, and make sure you understand why it works.
More HackerRank solutions All coding problems MCQ practice
See everything in one place: Coding Problems and Solutions
Comments
Post a Comment