Solved

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

Posted on 2011-09-07
10
368 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

728 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