Can someone explain string formatting in java and also explanation for below code on how it will work
System.out.printf( "%-15s%03d %n", s1, x);
Can someone explain string formatting in java and also explanation for below code on how it will work
System.out.printf( "%-15s%03d %n", s1, x);
If you run it with some given values you will get something like:
for: System.out.printf( "%-15s%03d %n", "string1", 123);
you get: 'string1 123 ' (without the '' of course and followed by an empty line)
As you can see, the first format will complete until 15 chars if the string is less then 15, for the second parameter, if it is less then 3 digits, it will add 0's in front of the number to match the 3 digits. After a space, you will get a new line too.