aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes examlab diagnostic test question 10 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "examlab diagnostic test question 10" Watch "examlab diagnostic test question 10" New topic
Author

examlab diagnostic test question 10

Fritz Guerilus
Ranch Hand

Joined: Jun 20, 2009
Posts: 65
Hi,
Can anyone help explain this output?


OUTPUT:
"1213%1$s"

Here's the explaination from exam lab:
"When using the printf method, the String formatting will be done for the first argument only. Other arguments will be considered as normal Strings, and they will not be formated"

What happended to "14%1$s" ?
How do you figure out which is the 1st argument?
In chapter 6's section on formating in K&B SCJP6 book, it doesn't show formating arguments for the printf() and format() methods in quotes("").

thank you
-Fritz


SCJP 6.0
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
Hi,

Look here: http://java.sun.com/j2se/1.5.0/docs/api/java/io/PrintWriter.html#printf(java.lang.String, java.lang.Object...)
and here: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html#syntax
Fritz Guerilus
Ranch Hand

Joined: Jun 20, 2009
Posts: 65


Thanks Ireneusz. I looked up the links but I still cant seem to figure it out, but I'll continue to research it.
In the meantime, does anyone have a simple explaination of this issue?
-Thanks
-Fritz
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

Fritz, try to create a test program to see how the indexes work in the printf method, that will clear your doubt about how indexing works



SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
indra negi
Ranch Hand

Joined: Aug 12, 2009
Posts: 68
but where are the args specified in the printf statement.



Here what I can get is only formats are specified.

I have understood what Ankit wants to say by using the following statement
Akanksha Mittal
Greenhorn

Joined: Jul 29, 2009
Posts: 26
but where are the args specified in the printf statement.

view plaincopy to clipboardprint?
System.out.printf("12%1$s", "13%1$s", "14%1$s");
System.out.printf("12%1$s", "13%1$s", "14%1$s");

Here what I can get is only formats are specified.

I have understood what Ankit wants to say by using the following statement
view plaincopy to clipboardprint?
System.out.printf("%1$s","a","b");
System.out.printf("%1$s","a","b");




PrintStream java.io.PrintStream.printf(String format, Object... args)
Only the 1st argument is considered for formatting, rest all args are normal args.

try this:
System.out.printf("4","%1$s","b", 1, 2);

This will only print 4.
indra negi
Ranch Hand

Joined: Aug 12, 2009
Posts: 68

Akanksha then in the following snippet the output should only contain 12 and rest should have been discarded. Why does it take "13%1$s" in the output alongwith 12.




Please explain.
Piyush Porwal
Ranch Hand

Joined: Apr 09, 2008
Posts: 30
%1 in the 'format' argument means replace %1 with first argument.


-Piyush Porwal (Junk box)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: examlab diagnostic test question 10