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

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

 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.


OR


"%-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.

Comments

Popular posts from this blog

Day2 : Compiling Java Program with Examples

Day1: Introduction to Java and its feature