Solved

What is this formatting? System.out.format...

Posted on 2011-09-07
10
343 Views
Last Modified: 2012-05-12
System.out.format(" %1$02x", ..... );

Open in new window


Can anyone explain what is this formatting?
0
Comment
Question by:humansg
  • 8
  • 2
10 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36500586
It is probably System.out.printlf formatting the same a in C languhge
look ath these examples:
http://www.java2s.com/Code/JavaAPI/java.lang/System.out.printf.htm
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36500589
this is format struing syntaz
the same for System.out.printf or Systemm.out.format

http://download.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html#syntax
0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 36500597

Argument Index

especially look at this in your case
The argument index is a decimal integer indicating the position of the argument in the argument list. The first argument is referenced by "1$", the second by "2$", etc.

Another way to reference arguments by position is to use the '<' ('\u003c') flag, which causes the argument for the previous format specifier to be re-used. For example, the following two statements would produce identical strings:

       Calendar c = ...;
       String s1 = String.format("Duke's Birthday: %1$tm %1$te,%1$tY", c);

       String s2 = String.format("Duke's Birthday: %1$tm %<$te,%<$tY", c);

So it means that the first varuiable should be printed as heaxadecimal - two charctes - that;'sa my understanding:
System.out.format(" %1$02x", ..... );


     
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 47

Expert Comment

by:for_yan
ID: 36500625
So you see if I print 16 it will print 10 (in hexadecimal - two positoions, as exopected)

          int mynum = 16;
System.out.format(" %1$02x",mynum );

Open in new window


Output:
 10

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36500628


And this is how it prints number 11 in hexadecimal - occupying two positions
          int mynum = 11;
System.out.format(" %1$02x",mynum );

Open in new window


 0b

Open in new window

0
 

Author Comment

by:humansg
ID: 36500748
Hi for_yan,

this is the formatting syntax.
%[argument_index$][flags][width]conversion

"%1$02x"
[argument_index$] is 1$    (which is the first argument <- understood)
[flags] is "0"                        (what is this flag about?)
[width] is "2"                       (2 character length?)
conversion is "x"                 (which is to hexadecimal base 16 <- understood)

The description used for flag is "The optional flags is a set of characters that modify the output format. The set of valid flags depends on the conversion."
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36500758
Yes, 2 is charcter length for sure, I think zero is not a flag, but just part of the width 02 -  ?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36500761


Flag  General  Character  Integral  Floating Point  Date/Time  Description  
'-'  y  y  y  y  y  The result will be left-justified.  
'#'  y1  -  y3  y  -  The result should use a conversion-dependent alternate form  
'+'  -  -  y4  y  -  The result will always include a sign  
'  '  -  -  y4  y  -  The result will include a leading space for positive values  
'0'  -  -  y  y  -  The result will be zero-padded  
','  -  -  y2  y5  -  The result will include locale-specific grouping separators  
'('  -  -  y4  y5  -  The result will enclose negative numbers in parentheses  

0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 500 total points
ID: 36500765
No, actually in this case flag is zero - result will be zero-padded - indeed "0b"
0
 

Author Closing Comment

by:humansg
ID: 36500769
Yeah, I got that too!
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to monitor the number of connections/threads being utilized on Tomcat 6.0 8 70
servlet example 11 40
tomcat administrtor 12 46
hibernate example using maven 12 41
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now