Django
Django

Reputation: 81

What does System.out.printf( "%-15s%03d\n", s1, x) do?

String s1=sc.next();
int x=sc.nextInt();
System.out.printf( "%-15s%03d\n", s1, x);

Can someone explain the part inside the 'printf' braces?

Upvotes: 3

Views: 64956

Answers (7)

Knight_Tinker
Knight_Tinker

Reputation: 1

System.out.printf("%-15s %03d %n", s1, x); is a format specifier for string To get the left-justified column, you need a percentage symbol, a minus symbol, the number of characters, and then the letter s (lowercase). So %-15s means fifteen characters left-justified.

d is format specifier for integers. To get 3 digit int 03 is used with %d

To get a newline %n is used

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

}

This is an explanation from omhambire08 from HackerRank that explains it really well.

Upvotes: 0

Sanjay Kotabagi
Sanjay Kotabagi

Reputation: 73

This is used to print to stdout in java.

String s1=sc.next();
int x=sc.nextInt();
System.out.printf( "%-15s%03d\n", s1, x);

Explanation:

  • s1, x is the string and integer you read
  • System.out.print used for stdout
  • "%-15s%03d\n", s1, x: here % prints the string, -15s gives 15 white spaces and %03d print the read integer in 3 digits (even though you read 2 or 1 or 5 it makes it 3 digits and print)

Upvotes: 1

Ruchika Sharma
Ruchika Sharma

Reputation: 1

("%-15s%03d%n", s1, x)

  • %: used as a formatter.
  • '-': minus sign used for left indentation of the string.
  • 15s: here 15 denotes the string's minimum field width 15.
  • '0': pads the extra 0s in the integer.
  • 3d: here 3 denotes integers minimum field width 3.
  • %n: prints the new line.

Hope, this helps!

Upvotes: 0

28rox
28rox

Reputation: 11

This is an syntax of String formatted and it can be only done with printf method in java.
as we already know we use "s" for string and "d" for number and here we can see in the syntax they have used "-" (space/white-spaces) followed by digit "15" it means the output will be with 15 white-spaces for a given string (from the left side) and in the same syntax we can see "03d" , meaning that, if necessary, zeros are added.

Upvotes: 1

Muhammad Tahsin Amin
Muhammad Tahsin Amin

Reputation: 31

"%-15s" means that within 15 blank space, the String "s1" will be filled in the left. (fill in the blanks from the left) "%03d" means that within 3 0s, the integer"x" will be filled in the right.(fill in the zeros from the right).

This can only be done by using printf method.

Upvotes: 3

Все Едно
Все Едно

Reputation: 726

Best tutorial for printf out there

The basics are: %s is looking for the first string as an argument witch it can find, %d for the first int, and 03 before %d is a modifier on how exactly you want to print the decimal number.

Upvotes: 2

Leon
Leon

Reputation: 3036

This is Java's formatter syntax. You can find more about it here. In your case, you have 2 parameters, that get formatted.

First s1 which is formatted using %-15s. The % means that what follows is an argument that will be formatted. Then follows a - resulting in left alignment. 15 fills the string up to a length of 15 characters (adding spaces at the end). Finally the s means, that you are formatting a string.

Second x which is formatted using %03d. Here the 0 is the fill character, meaning that, if necessary, zeros are added. The 3 is again the width, meaning the fill character 0 is added as many times as necessary to make it 3 digits long (this time at the beginning). Finally d means, that a integer is formatted.

Upvotes: 16

Related Questions