0

I am trying to use formatted output and would like to know how would I use %10s in this code

package formattedoutput;

public class FormattedOutput {

  public static void main(String[] args) {

    double  amount;
    amount = 43.676;
    int spaces;
    spaces = 77;
    String adding; 
    adding = "check me out";



    System.out.printf( "%1.2f%n", amount ); 

    System.out.println();  

    System.out.printf("%12d", spaces );

    System.out.println();

    System.out.printf("%10s", adding );
  }

}

From what I did I don't see any difference it says that "The letter "s" at the end of a format specifier can be used with any type of value. It means that the value should be output in its default format, just as it would be in unformatted output. A number, such as the "10" in %10s can be added to specify the (minimum) number of characters. " Am I using it properly ? If so why it says "s" specifier can be used with any type of value ?

3
  • Because it can treat any object as a string, by using the toString method. Commented Feb 14, 2014 at 23:33
  • Does it mean I would use only in that case ? When I need to convert to String Commented Feb 14, 2014 at 23:34
  • The normal case is to use %s with an actual String parameter. You would do this when you want to pad it with spaces so that it fills a certain width, to make a report look pretty. You might also use this if you have a format string with more than one value to format. You wouldn't normally use just "%s" as the format string, because that is pointless. Commented Feb 14, 2014 at 23:41

2 Answers 2

1

%s in formatted output determines, that the value should be printed out as a String, you can use it for numbers or any Object as well, the string value of an Object is determined by calling its toString method. Every object has one.

The number before s, such as %10s determines the minimum number of characters, that will be outputted. If the input string is shorter than the given number of characters, it will be prepended with spaces. This is used for example to align text to the right, such as this:

System.out.printf("%s\n", "id");
System.out.printf("%s\n", "name");
System.out.printf("%s\n", "rank");

outputs

id
name
rank

while

System.out.printf("%5s\n", "id");
System.out.printf("%5s\n", "name");
System.out.printf("%5s\n", "rank");

gives

   id
 name
 rank
Sign up to request clarification or add additional context in comments.

Comments

0

%10s tells the formatter to put spaces in front of the String if it is smaller than 10 characters. That's why you see no difference. If you put %15s you ask the formatter to put enough spaces to fill 15 characters with your String, and it will in your example put 3 spaces in front of it.

5 Comments

with %10s in the output I didn't see difference but when I increased it to %15s I saw that spaces were put ? Why I didn't see that difference with %10s ?
This should not be posted as an answer, since it is a question. Please post as a comment.
Well because %10s says: "if the string is smaller than 10 chars then add spaces to make it 10 chars wide", since your string is 12 chars, it doesn't do anything. But putting %15s says "if the string is smaller than 15 chars then add spaces to make it 15 chars wide" which it did with success in your case !
Does it mean it will be used only when we need to align strings ?
@Serenity : not exactly, it can be used for a lot of different reasons but I mostly use it to align things when I print a lot of lines. It could be used to output a fixed-length csv file for example in which each field must be exactly 50 characters wide.

Your Answer

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