You are viewing a single comment's thread. Return to all comments →
can you please explain this line. thanks"("%-15s%03d%n", s1, x);"
("%-15s%03d%n", s1, x)
% : used as a formatter.
'-' : minus sign used for left indentation of the string.
15s : here 15 denotes the string's minimum field width 15.
'0' : pads the extra 0s in the integer.
3d : here 3 denotes integer's minimum field width 3.
%n : prints the new line.
Hope, this helps!
if our first input integer is 2384754 or second 5 so how we add 000000 to make this like 0000005.
%07d will add 000000 followed by 5
common sense. Haha
thank you soooooo much:)
They have asked for only numbers between 0 and 999 here so the %03d padding is enough here.
If you need more digits in your number, then you give padding as necessary; in your case, %07d.
if i want to add 1 in place of zero ,to make three digit number then what will be solution
is there no solution for this quesion
you can calculate the length of integer and then add 100 for two digit integer of add 110 to a single digit integer
can we use %03d in other languages .if yes then please can you name them?
Check out C - it also has printf
hey I'm actually unaware of this statement "%-15s%03d%n"
and can anyone help me? like explain this or send a website where it's explained. i read the comments but that's not enough for me i need to know the basics.
Thank you for sharing this great resource!
System.out.println( StringUtils.leftPad("5", 7, "0") );
what the code defines? & how explain? or give link for more details.
Awesome Explanation... I have used for the first time..
yeah of course :)
in the above code he was padding 0 ,
but if we have to pad 1 ;
how can we do it ?
same here thank you
I have also
same here, formatted outputs is a new terminology to me
same as well
yes getting to know new things tq. :)
lets keep it going till it croses the page lol XD
haha me too
I never knew printf can be used in Java also!
someone else? hahaha
This really helps a lot, thank you!
And guys... we're pretty far out right
not there yet... :)
New for me too, it was great learning about this!!
Seriously though this is fantastic thank you.
Does it works in IDE?
Have you tried anytime?
you can also use \n instead of %n for printing a newline. Thanks for your help.
It's important to note that the '\n' newline character also adds a linefeed at the end. If you do not want the linefeed you should use "%n".
--The Java Tutorial: Formatting
what is line feed ?
line feed mean.
to add a new line
In Java, \r is always carriage return, and \n is line feed.
Why \n and %n both works for this program ?
Can you give an example which helps to understand the difference between them ?
%n is portable between various platforms, the value emmitted from %n will suits underlaying platform, where as value emmitted by \n is same for all the paltforms.
yeah, i've used \n instead of %n and both work fine. Thanks.
how about \t ??
\t is for vertical spacing
\t is tab
\t is horizontal tab that is equivalent to 4 spaces at a go
i think tab gives only 3 spaces
tab is a tab. It could be presented as 3, 4 or more spaces. Depends on the text editor, browser, etc. But most cases it's 4 spaces
This is what official documentation saids:
A new line character appropriate to the platform running the application. You should always use %n, rather than \n.
thank you so much
It helped a lot! Nice explanation.
Nice explanation. Thank You.
what we will use for right indentation .?
thank you ! i learnt from u!
that was cool man using printf in java....
Thank you for the explanation.
here '-' minus sign is used for left indentation...
Can we use '+' for Right indentation if not then what should we do for Right indendation???
just dont use the '-' sign..
by default it is right
Why it is not working if I use println for this statement?
The Objective of the question is to use 'printf' statements. So during code testing, the JUnit Test looks only for the 'printf' statement.
how can we print justified output using "println"
thank you very much :)
What does s and d indicate?
s indicates string and d indicates integer.
s indicates string and d indicates double integer
i have use it first time
why i can't go to this way
here i am using println intead of %n for new line
15 denotes the string's minimum field width 15. I didnt get this will you plz explain
what is x used for
Great explanation!! Thanks :)
new thing to be learnt
What is a formatter? and why do we have 3 formatters but only 2 variables (i.e s1 , x)?
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:
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.
if s1 = hi then it will produce:
s1 = hi
"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.
if x = 70 then it will produce:
x = 70
"070" // The result is 3 characters long, with a '0' filling the unused space
Time to watch some tutorial videos I guess! Good luck.
superb expelnation man
clear explanation :)
thank you for this awesome explanation
but how the spaces between string and integer are denoted in program
Did you get an answer? I had the same question about the space in the centre
Can you tell me please, where i can find some serious TuT Videos?
I would love to learn this kind of things. Between, thanks for the clear explanation!
Awesome explanation Bro!!
you cleared my doubt, thank you
Thank you for the clear explanation!
good explanations learned something new
what is the use of % ?
Default padder " " will always be in the middle? Keeping the string and int on the ends.
good explination bro
thank you superb explanation
perfect explanation !
amazing explanation. thanks a lot !
Thanks for your explaination. It really helped. It was clear and the best part was that you expained it by breaking it in parts... Every letter in that segment.
Best explanation on this thread, thanks a lot.
Thank you very much!!!
Very good explanation, I hope I can be better at understanding the syntax and implementing it in the future.
Thanks for the explanation ...
it is very useful and clearly explained @thens
plz can u give some alternative solution for this
what is the difference between println(or print) and printf??
why you used print f instead of printrline
Thanks!! for your explanation about this exercise
Thanks for the Clarification..!
which sign is used for right indentation of the string?
%-15s --> for string with left indentation with length 15
%03d --> for integer with length and padded with zero
Hi, Thanks for this.
If I need to '1' instead of '0' for integer, then how to modify this as
("%-15s%13d%n", s1, x) is not producing desired o/p.
Would appreciate if you may help with this.
can u do it with string concatination
can u do it using string concatination
Awesome, but java turn this really ugly, for god....
thank you so much..
Gosh, I was trying to use loops for it to find out how many spaces I need to add, and this turns out to be such a simple thing....
Thanx for such a pleasant explanation.
thanks nice explanation
thank you ,i'm using this first time
if we integer is 5 and we used %-08s then o/p will be 00000005
So "%n" can also be used to print new line, instead of "\n". Thank you for the explanation.
thank you so much sir
Sir, if i forgot these code then you have any other example to solve this condition?
first time i see this type of concept
yes it helps..thanks.. :)
I could not find it on here:
s - for String
d - for double
f - for float
i - for int(?)
Okay now clearer. Are there any list? Table ? About this ?
Thanks, i got it.
this one is really cool. :)
Thank you bro
Kindly explain what does the 's' 'd' & 'n' denotes in "%-15s%03d%n"
Thank you so much
super clear explaination
Thank you very much for the details.
why we use printf instead of println
from where did you read this output formatting ..can you send me a link
Thanks a lot, explained well.
("%-15s%03d%n", s1, x);
in this statement n mean ???
%n is for new line
thanks , really obivious
If we want to have right indentation of string?
Thanks a lot .This was really a new topic for me and You explained it very clearly.Thanks Again:)
with declaring 15 as min field width, can we use a string with more than field width
thank you soo much man!
@thens do you know any tutorial regarding this string formatting methods?
And how do you mark a value as right indented?
Thanks man it's really help for newbie programmer
Thanks for explanation. Can you suggest me some resourse to learn string formatting please.
Wht if we wanted to right indentation?Would the minus be after d?But seriously though thanks for the explaining it
why u are using , insteadof java uses + operator.please explain.
printf means printing with formatiing. While println means printing something in a specific line.
Printing with printf will end in same line, while println will take you to next line after the printing is done.
Hope that helps....
Thank You Sagar
I don't know why this has been downvoted so much. It's a very valid question and I'm glad you asked it.
Cant we add space between %-15s & %03d
thank you so much
in this line there is a %-15s this code is make exactly 15 character from strart of statment and the %03d this code make if your number is less than 3 decimal it put 0 at the first of number and then n for new line
you guys should reference this site.
access to this link is denied
https://www.baeldung.com/java-printstream-printf Check this link bro