• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

printf

 
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 80665
478
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 80665
478
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 2237
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 80665
478
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Forgot about zero. Thank you.
 
Ranch Hand
Posts: 1004
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the real world coding i never come across these kind of scenarios SCJP asks
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic