File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

printf, multiple flags

 
Paul Stat
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • 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?

 
M Srilatha
Ranch Hand
Posts: 137
  • 0
  • Mark post as helpful
  • send pies
  • 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!
 
Sagar Rohankar
Ranch Hand
Posts: 2904
1
Java Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • 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: 2904
1
Java Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Aakash Goel
Ranch Hand
Posts: 198
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In printf, does the does width have any significance?
 
Aakash Goel
Ranch Hand
Posts: 198
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for floats, that is.
 
Michael Angstadt
Ranch Hand
Posts: 277
Eclipse IDE Java PHP
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic