-4

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);
Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
  • 3
    What specifically do you not understand about it? Did you read the docs? – GBlodgett Nov 19 '18 at 18:29
  • Possible duplicate of [How to read / convert an InputStream into a String in Java?](https://stackoverflow.com/questions/309424/how-to-read-convert-an-inputstream-into-a-string-in-java) – E.Coms Nov 19 '18 at 19:02
  • Your request for someone to _"explain string formatting in java"_ is too broad for Stack Overflow. Instead you should ask a specific question about a specific problem you have with string formatting in Java. – skomisa Nov 20 '18 at 01:23
  • @E.Coms How is that suggested duplicate related to this question?! – Mark Rotteveel Nov 20 '18 at 12:23
  • My point is how formating is happening in the sense how this number work inside printf -15s and 03d – Alavala Mahesh Nov 20 '18 at 14:54

1 Answers1

2

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.

Andrea Calin
  • 308
  • 2
  • 10