HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Output two columns

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
twooutputcolumns

Problem

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);
        System.out.println("================================");
        for(int i = 0; i < 3; i++){
            String text = sc.next();
            int number = sc.nextInt();
            printRowOutlined(text, number);
        }
        System.out.println("================================");
    }
}

Solution

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.

Code Snippets

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

Context

StackExchange Code Review Q#139964, answer score: 6

Revisions (0)

No revisions yet.