File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
A friendly place for programming greenhorns!
Big Moose Saloon
Register / Login
Programmer Certification (SCJP/OCPJP)
Joined: Jun 04, 2007
Apr 26, 2010 16:41:20
i can't seem to get the hang of printf. how did you guys memorize all the combinations?
int i2 = 12345; System.out.printf(">%1$< \n", i2); //error at runtime int i2 = 12345; System.out.printf(">%1$d< \n", i2);//fine at runtime
...do we always need to have d in the end?
Joined: Mar 07, 2010
Apr 26, 2010 17:44:34
following are the characters those are required for outputting specific type of values
d for integers
b for booleans
s for strings
c for character and
f for float
and yes we have to give those character for respective values because the compiler gives error at runtime
SCJP 6 [86%], OCPWCD [84%], OCEJPAD [83%]
If you find any post useful, click the "plus one" sign on the right
Joined: Apr 23, 2010
Apr 26, 2010 18:02:32
Yes, that's right.
First have a look to the easiest printf.
All what you need is a % and a letter described above.
int i2 = 12345; System.out.printf("%d", i2);
double d2 = 123.456; System.out.printf("%f", d2); // float and double need a f
Then you can add other signs.
System.out.printf("%07f", d2); // leading 0 and a length of 7
Do you want to have less digits after the dot, use
System.out.printf("%07.2f", d2); // leading 0 and a length of 7
and so on
$1 : use the first parameter
SCJP 6, SCWCD 5
Creator of Enthuware JWS+ V6
Joined: Apr 07, 2010
Apr 27, 2010 04:38:41
You can check the API documentation:
and choose the class java.io.PrintStream.printf(
format, Object... args)
If you have read that, go further on the format section:
format string format
I agree. Here's the link:
subject: printf confusion
Formating with printf() and format()
String formatting doubt
Question on Pattern Matching
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter
| Powered by
Copyright © 1998-2015