Solved

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

Posted on 2011-09-07
10
340 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java array passed to SQL where clause 23 62
Image decoding from Camera 3 72
java 8 lambda expresssions exception handling 3 75
fibonacci ten numbers 4 28
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

910 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

22 Experts available now in Live!

Get 1:1 Help Now