• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt on page 547 HFSJ

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
out.print(String.format(ATTR_TEMPLATE, “name”, this.name));
out.print(String.format(ATTR_TEMPLATE, “size”, this.size));

private static final String ATTR_TEMPLATE = “%s=’%s’ “;
private static final String OPTION_TEMPLATE= “ <option value=’%1$s’> %1$s </option>”;



but according to String API format function can,t take (String, String, String)

can anybody tell me what is going on ??

thanks in advance..
 
Ranch Hand
Posts: 170
Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is the format method signature:
format(String s, Object... args)

which mean the first argumet is a String
and a random number of Object argument
that are to be formmated in the String.

for example:
String name="Jonathan";
format("my name is %s",this.name); //print "my name is Jonathan"

the number of var-args (u did SCJP5, didn't you?) is unknown
till runtime.
since the format Strings (the 2 final Strings) have 2 arguments,
you need to send 2 Object (Strings in this case) to put there...

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html
 
ankit kumar
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ya this is correct that String.format takes argument that you have mentioned
but what I have posted is mentioned in the book . to run that code what i have to do .



 
Jonathan Elkharrat
Ranch Hand
Posts: 170
Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you have java 1.5 and above it should compile just fine...
 
ankit kumar
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See what java1.5 API says

format(Locale l, String format, Object... args)
Returns a formatted string using the specified locale, format string, and arguments.

String format(String format, Object... args)
Returns a formatted string using the specified format string and arguments.


 
Jonathan Elkharrat
Ranch Hand
Posts: 170
Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so? the String is also an Object...
it's just like printf().
(and you should know that if you passed SCJP5)
 
ankit kumar
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you have not have a look on the signature .

First agument should be Locale but in the book String is passed.

And the third agument should be array of the object not a single object or String ..
 
Jonathan Elkharrat
Ranch Hand
Posts: 170
Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
they use the oveloaded method that doesn't take a local.
and in varArgs place you can put zero, one, or any other number of Objects...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic