File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes printf, multiple flags Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "printf, multiple flags" Watch "printf, multiple flags" New topic
Author

printf, multiple flags

Paul Stat
Ranch Hand

Joined: Jan 20, 2009
Posts: 50
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

Joined: Aug 27, 2008
Posts: 137
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!


Thanks,<br />Srilatha M
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

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]


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

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

Joined: May 26, 2008
Posts: 198
In printf, does the does width have any significance?


SCJP 5 95%
SCJP FAQ | SCJP Mock Tests | SCJP Tipline | Generics
Aakash Goel
Ranch Hand

Joined: May 26, 2008
Posts: 198
for floats, that is.
Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 274

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.


SCJP 6 || SCWCD 5
Aakash Goel
Ranch Hand

Joined: May 26, 2008
Posts: 198
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: printf, multiple flags