About Question enthuware.ocpjp.v7.2.1622 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
sinapse
Posts: 27
Joined: Sat Aug 31, 2013 5:38 pm
Contact:

About Question enthuware.ocpjp.v7.2.1622 :

Post by sinapse »

System.out.printf("%1$s %2$s %s", "A", "B", "C");

"In this case, it starts with the last %s. Therefore, it prints the first argument which is A."should

If it is like you say it should print "A" "A" "B".

Can you help me here ? This is the hardest form to understand...

sinapse
Posts: 27
Joined: Sat Aug 31, 2013 5:38 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1622 :

Post by sinapse »

And this should print:

System.out.printf("%2$s %2$s %s", "A", "B", "C");

A B B

which is not true .!

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1622 :

Post by admin »

sinapse wrote:System.out.printf("%1$s %2$s %s", "A", "B", "C");

"In this case, it starts with the last %s. Therefore, it prints the first argument which is A."should

If it is like you say it should print "A" "A" "B".

Can you help me here ? This is the hardest form to understand...
There are three formatters in the printf:
1. %1$s : This uses explicit indexing. So it will print the argument at that given index, which is 1. So it will print A.

2. %2$s : This uses explicit indexing as well. So it will print the argument at that given index, which is 2. So it will print B.


and 3. %s : This uses ordinary indexing. Also, this is the first place where ordinary indexing is being used, so the ordinary index will start with 1 here. So it will print the argument at 1. So it will print A.


HTH,
Paul.

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1622 :

Post by admin »

sinapse wrote:And this should print:

System.out.printf("%2$s %2$s %s", "A", "B", "C");

A B B

which is not true .!
Not sure how you are interpreting the explanation. It says, "the ordinary index starts with the first format specifier that does not use explicit index."

In your example, ordinary index starts from the third piece of the whole string "%2$s %2$s %s" i.e. %s. So it will print A. The first two i.e. %2$s %2$s are using explicit indexing with a value 2, so they will print B and B. Thus, the whole string will print. B B A.

HTH,
Paul.

EpicWestern
Posts: 17
Joined: Wed Jan 22, 2014 12:35 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1622 :

Post by EpicWestern »

I had this same problem but figured out. Its confusing because the language seems to imply that ordinary indexing gets priority over explicit indexing, and that "it starts with the last %s" refers to the indexing process as a whole not specifically the ordinary indexing part.

Also "Therefore, it prints the first argument which is A" adds to that confusion since it fails to mention that two other values were printed out first.

ThufirHawat
Posts: 28
Joined: Wed Feb 25, 2015 9:03 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1622 :

Post by ThufirHawat »

This is poor documented by Java (in my opinion).
Tnx for Paul, I understood here only.

Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests