• Post Reply Bookmark Topic Watch Topic
  • New Topic

printf  RSS feed

 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is said about flags in pritf in SCJP book,page 507



"+" Include a sign (+ or -) with this argument



I don't think this flag"+" could print negative numbers as book said.Which flag can print negative sign?


Another question:


about this line:



What do this mean: +-7?

When I try



It throws exception.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abalfazl hossein wrote:
"+" Include a sign (+ or -) with this argument


I don't think this flag"+" could print negative numbers as book said.Which flag can print negative sign?

Why not try it with a negative number and see what happens.




What do this mean: +-7?

The '+' does as detailed above, it appends a sign character to the number.
The '-7' means pad to 7 characters and left align, specifically '-' means left align but this is only meaningful if you specify a width to pad the number to (numbers are naturally right aligned), hence you get an exception if you use it without a width.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use \n even if you found it in your book. Always use %n
You can find all the tags in the Formatter class. Beware: there are many many different tags.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You always get some sort of sign of a number being negative. What the + option means is it prints a sign when the number is positive, too.
 
Paweł Baczyński
Bartender
Posts: 2076
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You always get some sort of sign of a number being negative. What the + option means is it prints a sign when the number is positive, too.

Or zero.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forgot about zero. Thank you.
 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the real world coding i never come across these kind of scenarios SCJP asks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!