212k views
5 votes
Assume the following declaration exists in a program:

String name = "James";
Which format specifier should be inserted in the statement below to display the value of name in a field that is 20 spaces wide?

System.out.printf("_______", name);
A. %-20.2d
B. %20.f
C. %20s
D. %20w

Please select the correct format specifier to fill in the blank in the statement above.

User Navra
by
8.4k points

1 Answer

4 votes

Final answer:

The correct format specifier to fill in the blank is %-20s.

Step-by-step explanation:

The correct format specifier to fill in the blank in the statement above is %-20s. The %-20s format specifier is used to display a String value in a field that is 20 spaces wide. The %-20 part specifies the width of the field, and the s indicates that the argument is a String.

Example:

String name = "James";
System.out.printf("%-20s", name);

This will display the value of name in a field that is 20 spaces wide.

User Mozart
by
8.6k points