-3

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.

can someone explain the System.out.printf("%-15s%03d%n", s1, x);

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();
            System.out.printf("%-15s%03d%n", s1, x);
        }
        System.out.println("================================");

}

}
7
  • 4
    That's a printf, just like String.format or System.out.format. Check the format String Syntax
    – AxelH
    Jun 15, 2017 at 12:01
  • 1
    System.out.printf seems to work like C's printf function. More info here: cplusplus.com/reference/cstdio/printf Jun 15, 2017 at 12:01
  • 3
    just edit it my previous comment. And by the way, that's NOT a println! That question is asking to define a syntax that is well documented. So for me, this is off-topic because it can't be clearly explained here and mostly because this is asking for guide
    – AxelH
    Jun 15, 2017 at 12:03
  • 1
    Did you search for the answer before posting the question ?
    – c0der
    Jun 15, 2017 at 12:04
  • 1
    Possible duplicate of How to format text correctly using printf() (Java)
    – c0der
    Jun 15, 2017 at 12:06

2 Answers 2

7

Basically every %... is gonna be replaced by one of the arguments of printf. What is after the % sign is a format specifier.

In %-15s:

  • - means: left-justified
  • 15 means: if the result is less than 15 characters long, add spaces until it is 15 characters long
  • s means: convert the parameter into a string with toString and use the result

In %03d:

  • 0 means: pad with 0s instead of spaces
  • 3 means: make it at least 3 characters long
  • d means: the argument will be an integer number, format it as a base-10 number.

%n is the same as \n on *NIX or \r\n on Windows.

You will get more info here: https://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax

EDIT based on remarks by AxelH and Andy Turner

6
  • 1
    s or S means much more : "If the argument arg is null, then the result is "null". If arg implements Formattable, then arg.formatTo is invoked. Otherwise, the result is obtained by invoking arg.toString().". So you could pass a lot of think as an argument ;)
    – AxelH
    Jun 15, 2017 at 12:09
  • "use it verbatim" not quite: it means insert the string which is the result of calling Objects.toString on the parameter. Jun 15, 2017 at 12:10
  • 1
    And the correct syntax for Java is there. Because the printf of c++ is not necessarly going to stay the same
    – AxelH
    Jun 15, 2017 at 12:10
  • 1
    "outputs nothing" no, it outputs the system dependent newline character sequence. Jun 15, 2017 at 12:11
  • 1
    A last note, \n is system dependent, %n is better because it will use the platform-specific line separator. So there a different. EDIT : Well, you catch that too. Nice and complet answer
    – AxelH
    Jun 15, 2017 at 12:27
2

Its Java formatter syntax

first half - %-15s

  • % - says that what follows is an argument that will be formatted.
  • s - says youre formatting a string
  • 15 - number of characters you put into string
  • and finally - means string is gonna be justified to the left

second half - %03d

  • d means youll be adding integers
  • 0 means youll be adding 0's where necessary
  • 3 means you need to add 3 digits

  • %n is System.line_separator - basically outputs new line. It does the same as /n but %n is portable across platforms (credit @AxelH)

2
  • @AxelH minor interruption @ my side, completed :) Jun 15, 2017 at 12:20
  • 1
    I meant %n actually ;) the System.line_separator flag. A good answer should be complete ;)
    – AxelH
    Jun 15, 2017 at 12:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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