Output formatting:

Input Format

Every line of input will contain a String followed by an integer. Each String will have a maximum of 10 alphabetic characters, and each integer will be in the inclusive range from 0 to 999.

Output Format

In each line of output there should be two columns: The first column contains the String and is left justified using exactly 15 characters.
The second column contains the integer, expressed in exactly 3 digits; if the original input has less than three digits, you must pad your output's leading digits with zeroes.

Sample Input

java 100
cpp 65
python 50

Sample Output

java           100 
cpp            065 
python         050 

printf is pretty straightforward. Two formats per row, newline at the end. The lines that don't need to be formatted (first and last) can be put on the screen using println instead. To the best of my knowledge it isn't possible to put both printf statements in one printing statement, so I made it a function instead. Keeps things neat.

My naming is probably horrible. As usual.

import java.util.Scanner;

public class Solution {
    private static void printRowOutlined(String left, int right) {
        System.out.printf("%-15s", left);
        System.out.printf("%03d\n", right);
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        for(int i = 0; i < 3; i++){
            String text = sc.next();
            int number = sc.nextInt();
            printRowOutlined(text, number);

To the best of my knowledge it isn't possible to put both printf statements in one printing statement, so I made it a function instead.

It is possible: the printf method takes a varargs as second argument, so you can give it multiple arguments to format. When you have multiple arguments, each parameter can be refered to as %[argument_index$] in the format String (as per Formatter Javadoc), where argument_index is the index of the argument in the varargs. In this case, we can have:

private static void printRowOutlined(String left, int right) {
    System.out.printf("%-15s%03d%n", left, right);

Since each parameter in the format String are refered to in the order in which they are given in the arguments, we don't even need to specify the index (but we could have "%1$-15s%2$03d%n" as the format String). Regardless of that, I think having a separate method for the printing operation is still cleaner.

Also, don't use \n. You can use the %n specifier, which will use the line separator of the current system.

Furthermore, I notice you're not doing any validation on your input, you could verify that the integer is correct, in the right range, and that the String to format has the right numbers of characters.

  • \$\begingroup\$ Great advice :-) \$\endgroup\$ – Mast Aug 29 '16 at 22:18

There happen to be 3 lines of input in the given example, but the problem description doesn't mention that there will always be exactly 3. The program won't behave well with a different number of lines. Excess lines would be simply ignored, but if there are less 3 lines, the scanner will throw a runtime exception.

A Scanner has a hasNext method that can help make this work with any number of lines of input, if you change the for-loop to this:

while (sc.hasNext()) {

As for naming... I usually try to err on the side of simplicity and naturalness. For example I'd call an instance of Scanner a scanner, and a method that prints columns printColumns, or printPaddedColumns, or even simply print.

The formatting style leaves something to be desired... I suggest to copy-paste into an IDE like IntelliJ or Eclipse and use the Reformat Code feature and observe what it changes. It will add spaces around operators and such, resulting in something slightly easier to read.

Instead of printing the === twice, I'd use a helper function. I'd go even further and add printHeader and printFooter helpers that can both call the same thing. It's a small thing, but it would make the main function higher level, delegating lower level details such as header and footer formatting elsewhere, out of the way of the bigger picture.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.