We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
If you imagine a coaster (as in the little bit of card where you put your cup when you're drinking something...) That's comparable to a formatter, in the sense that it shows the place you should put something.
There are only two formatters here: "%-15s%03d%n"
The one that you think is the third: %n actually represents a linebreak (like hitting Return on your keyboard while typing a sentence).
%s=String// a string goes here%d=int(digit)// an int goes here%f=float// a float goes here
Let's look at the whole statement:
("%-15s%03d%n",s1,x)
The two formatters are: %-15s which is where the variable s1 (a String) is placed. %03d which is where the variable x (an int) is placed.
The other parts of those formatters mean different things...
The -15 part means that: -: when s1 is put into this spot, keep it to the left side. 15: make sure it takes up 15 spaces.
EXAMPLE
if s1 = hi then it will produce:
"hi "// Note that the string is 15 characters long
The 03 part means that when x is put into this spot: 3: it must take up three spaces. 0: any spaces it doesn't use are to be filled with 0's. N.B. The default padder is whitespace " " if 0 is not used.
EXAMPLE
if x = 70 then it will produce:
"070"// The result is 3 characters long, with a '0' filling the unused space
Time to watch some tutorial videos I guess! Good luck.
Java Output Formatting
You are viewing a single comment's thread. Return to all comments →
The formatter is a placeholder.
If you imagine a coaster (as in the little bit of card where you put your cup when you're drinking something...) That's comparable to a formatter, in the sense that it shows the place you should put something.
There are only two formatters here:
"%-15s%03d%n"
The one that you think is the third:
%n
actually represents a linebreak (like hitting Return on your keyboard while typing a sentence).Let's look at the whole statement:
The two formatters are:
%-15s
which is where the variables1
(a String) is placed.%03d
which is where the variablex
(an int) is placed.The other parts of those formatters mean different things...
The
-15
part means that:-
: whens1
is put into this spot, keep it to the left side.15
: make sure it takes up 15 spaces.EXAMPLE if
s1 = hi
then it will produce:The
03
part means that whenx
is put into this spot:3
: it must take up three spaces.0
: any spaces it doesn't use are to be filled with0
's.N.B. The default padder is whitespace
" "
if0
is not used.EXAMPLE
if
x = 70
then it will produce:Time to watch some tutorial videos I guess! Good luck.