my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes printf method help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "printf method help" Watch "printf method help" New topic
Author

printf method help

Kamila Jolan
Greenhorn

Joined: Jul 12, 2010
Posts: 25
Hi ,

I have a problem understanding how the formatting in the printf method is working.
The following method:
gives the output: +07 252. Why it is not: +07,252? Why it does not display ","?
Also returns: > (123)<, Why it does not display a sign (-)?

Any help will be appreciated

Kamila
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

System.out.printf("%1$,+07d",7252);

have you checked the output by running it.it must show you.
and remember below thing while using printf
-ve means left justify this argument.
( - this wil put the negative numbers into the brackets.


SCJP6.0,My blog Ranchers from Delhi
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051


I have a problem understanding how the formatting in the printf method is working.
The following method:
gives the output: +07,252


Also
returns: > (123)< it will not show -ve because when we include "(" then it will put negative numbers into it.


Study chapter 6 last page before 2 minute drill for the K&B you will get the complete explaination of your query
Vicky Mehta
Greenhorn

Joined: Jul 15, 2010
Posts: 15
Minor correction in output
int i1 = -123; System.out.printf(">%1$(7d< \n", i1);

will be
Note 2 leading spaces before open braces


Preparing for SCJP
Kamila Jolan
Greenhorn

Joined: Jul 12, 2010
Posts: 25
Vicky, Shanky thanks for the answer.
I understand the second case, but still I don't get why in the in the first case the "," is not displayed.Could you elaborate more please?

Regards,

Kamila

Vicky Mehta
Greenhorn

Joined: Jul 15, 2010
Posts: 15
If the ',' ('\u002c') flag is given, then the locale-specific grouping separator is inserted by scanning the integer part of the string from least significant to most significant digits and inserting a separator at intervals defined by the locale's grouping size.

http://download.oracle.com/javase/6/docs/api/java/util/Formatter.html#syntax
, indicates that the digits in the number should use the default digit seperator of the system locale. It is well possible that the system locale of you computer is set to such locale in which the digit seperator is a space. hence, your display is like like that.
There are two options to change that:
1) Change default system locale in the system in Control Panel -> Regional and Language Options to such a locale in which the seperator is a comma. Remember to reinvoke the command prompt and then execute your program.
2) Invoke following printf
http://download.oracle.com/javase/6/docs/api/java/io/PrintStream.html#printf(java.util.Locale,%20java.lang.String,%20java.lang.Object...)
instead of
http://download.oracle.com/javase/6/docs/api/java/io/PrintStream.html#printf(java.lang.String,%20java.lang.Object...)
for the first parameter you can Locale as LOCALE.US for which AFAIK the seperator is a comma.
Kamila Jolan
Greenhorn

Joined: Jul 12, 2010
Posts: 25
Vicky thanks very much
 
Don't get me started about those stupid light bulbs.
 
subject: printf method help