This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Doubt on page 547 HFSJ Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Doubt on page 547 HFSJ" Watch "Doubt on page 547 HFSJ" New topic
Author

Doubt on page 547 HFSJ

ankit kumar
Ranch Hand

Joined: Apr 28, 2008
Posts: 108
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..

Cleared SCJP 1.5 | Cleared SCWCD 5.0
SCDJWS 6 in progress.....
Jonathan Elkharrat
Ranch Hand

Joined: Dec 31, 2008
Posts: 170

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


SCJP 5, SCWCD 5, SCBCD 5
ankit kumar
Ranch Hand

Joined: Apr 28, 2008
Posts: 108
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

Joined: Dec 31, 2008
Posts: 170

if you have java 1.5 and above it should compile just fine...
ankit kumar
Ranch Hand

Joined: Apr 28, 2008
Posts: 108
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

Joined: Dec 31, 2008
Posts: 170

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

Joined: Apr 28, 2008
Posts: 108
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

Joined: Dec 31, 2008
Posts: 170

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...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt on page 547 HFSJ
 
Similar Threads
unable to access the attribute in any of the container scope
Doubt in JSP Custom tags (Simple Tag) in Dynamic Drop Down
logic in dynamic-attribute
HFSF - page 547 - ATTR_TEMPLATE?
setDynamicAttribute not being invoked