• 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:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

printf, multiple flags

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So the format of a printf argument is like so

%[arg_index$][flags][width][.precision]conversion char

"-" left justify
"+" include a sign
"(" put negative numbers in parentheses
"0" pad with zeroes
"," include locale specific seperators

b: boolean
c: character
d: digit
f: float
s: string

the flags seems to imply you can have multiple flags, if however I try the following it causes an exception. Can only one flag be set at a time?

 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

It seems the combination of '-' and '0' is illegal.
Got to know from the API documentation.

Its given:
If both the '-' and '0' flags are given then an IllegalFormatFlagsException will be thrown.

Even i am not sure why this combination is illegal.

Refer to java API doc of Formatter class for more info!

Hope this helps!
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Stat wrote:
the flags seems to imply you can have multiple flags, if however I try the following it causes an exception. Can only one flag be set at a time?



It throws "IllegalFormatFlagsException: Flags = '-0'", and what are you doing, is something illogical and can't be used, like the flag "0", padded with zeroes, fills the empty left hand side space with zeroes and at the same time you are telling JVM, to display that numbers from left.. How can this two things possible at the same time, that's why "IllegalFormatFlagsException: Flags = '-0'" exception..

When I tried the other flag combination, It works..



[Wrong exception written]
 
Sagar Rohankar
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

M Srilatha wrote:
Its given:
If both the '-' and '0' flags are given then an IllegalFormatFlagsException will be thrown.



Particularly, its written here in JavaDocs.
 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In printf, does the does width have any significance?
 
Aakash Goel
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for floats, that is.
 
Ranch Hand
Posts: 282
Eclipse IDE PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aakash Goel wrote:In printf, does the does width have any significance?



If the length of your number exceeds the specified width, then it's the number's length that takes importance. So even though the first printf() statement specifies a width of 1, the number itself takes up 11 characters, so the width is ignored.
 
Aakash Goel
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Michael Angstadt wrote:
If the length of your number exceeds the specified width, then it's the number's length that takes importance. So even though the first printf() statement specifies a width of 1, the number itself takes up 11 characters, so the width is ignored.



Thanks a lot Michael. This happens with integers too!

Now I get the point.
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic