Avatar of humansg
humansgFlag for Singapore

asked on 

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

System.out.format(" %1$02x", ..... );

Open in new window


Can anyone explain what is this formatting?
Java

Avatar of undefined
Last Comment
humansg
Avatar of for_yan
for_yan
Flag of United States of America image

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
Avatar of for_yan
for_yan
Flag of United States of America image

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
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of for_yan
for_yan
Flag of United States of America image

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

Avatar of for_yan
for_yan
Flag of United States of America image



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

Avatar of humansg
humansg
Flag of Singapore image

ASKER

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."
Avatar of for_yan
for_yan
Flag of United States of America image

Yes, 2 is charcter length for sure, I think zero is not a flag, but just part of the width 02 -  ?
Avatar of for_yan
for_yan
Flag of United States of America image



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  

SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of humansg
humansg
Flag of Singapore image

ASKER

Yeah, I got that too!
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo