Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Doubt on page 547 HFSJ

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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...
 
You would be much easier to understand if you took that bucket off of your head. And that goes for the tiny ad too!
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic