• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

format()

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you help me in understanding the code on page No 491 Chapter 6 in Kathy Siera& Bert Bates ,please focus on 7d specially ,d is for integer but what 7 is for?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

If you post some of the code, more people would be able to help.
 
Mohammed shareef
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here is the code
int i1= -123;
int i2= 12345;
System.out.println(">%1$(7d< \n",i1);
System.out.println(">%0,7d <\n",i2);
System.out.println(">%+-7d< \n",i2);
System.out.println(">%2$b + %1$5d< \n",i1,false);
This produces output :
>(123)<
>012,345<
>+12345<
>false + -123<
In above code i can understand everything except the digit 7,what is the reason for putting 7 in first three line& 5 in the last line.can any one explain it? Thanking you in advance.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to the API for java.util.Formatter...

The format specifiers for general, character, and numeric types have the following syntax:

%[argument_index$][flags][width][.precision]conversion


So in the above examples, the 7 and the 5 specify width, which can be seen in this formatted output (using printf instead of println)...

Try increasing these numbers, and see how the output is affected.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic