Page 1 of 1

About Question enthuware.ocpjp.v7.2.1409 :

Posted: Sat May 11, 2013 6:13 pm
by renatumb

Code: Select all

System.out.printf(" \" $%.2f \" ", -23.20); // software's answer
System.out.printf(" \" $%+.2f \"  ", -23.20);  // my answer
Both will generate the same output but it considered my answer wrong

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

Posted: Sat May 11, 2013 9:23 pm
by admin
You are right. This is difficult to check. I have added this as a correct answer as well but there could be more :(
-Paul.

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

Posted: Thu Sep 19, 2013 4:18 am
by Wisevolk
The corrected answer is always "$%.2f", so from my opinion the you should correct either the answer "$23.20" or the statement "$%+.2f"

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

Posted: Thu Sep 19, 2013 8:10 am
by admin
Since it cannot show all the possibilities, it just shows one of the two. But if you enter any of the correct options i.e. $%+.2f; or $%.2f;, it will be marked as correct.

HTH,
Paul.

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

Posted: Mon Sep 23, 2013 5:56 am
by Wisevolk
Am I wrong if I say that "$%+.2f" would create an output of "$+23.20" ?

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

Posted: Mon Sep 23, 2013 7:45 am
by admin
Wisevolk wrote:Am I wrong if I say that "$%+.2f" would create an output of "$+23.20" ?
Did you try it out? What does it print?
-Paul.

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

Posted: Tue Sep 24, 2013 6:45 am
by Wisevolk
Ok you're write I had a bug vision, may be I could take some rest !! I saw a + in the answer instead of a minus !!

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

Posted: Fri Oct 18, 2013 10:32 am
by DesRenthuware
Hi.

I think this question has evolved. When checking the answers (via "Show correct answer"), a message pops up saying there are multiple answers, but only one is being shown. I assume this is because the original question asked people to input the formatting string to obtain the answer as opposed to predicting the output generated? This is no longer the case.

Cheers

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

Posted: Mon Nov 11, 2013 3:38 pm
by jeremy_dd
the '(' flag is also not very common, and not listed in the 'official' book....

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

Posted: Mon Nov 11, 2013 4:55 pm
by admin
jeremy_dd wrote:the '(' flag is also not very common, and not listed in the 'official' book....
But you need to know it for the exam.

HTH,
Paul.

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

Posted: Wed Aug 12, 2015 4:20 am
by monseigneur
hi mate,

could you please clarify the following:
in flags we have:
b. "+" Include a sign (+ or -) with this argument - Applicable only if conversion char is d or f (i.e. for numbers).
but, i reckon, negative sign (-) will always be included in the result if it is in the input (unless we use "(" flag), so, basically, the flag "+" indicates if we only include positive sign (+).

is this correct?

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

Posted: Wed Aug 12, 2015 5:16 am
by admin
No, + sign means you want to include both the signs i.e. + and - in the resulting string. See this table: https://docs.oracle.com/javase/tutorial ... ormat.html
You may also want to try it out and see what happens.

HTH,
Paul.

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

Posted: Wed Aug 12, 2015 7:02 am
by monseigneur
thank you, that is clear,

could you give a short example of negative sign not being included in the resulting string unless we use "+" flag, though?
i mean, let's say we have

Code: Select all

double num = -3.4;
System.out.printf("\"%+.1f\"", num);
System.out.println();
System.out.printf("\"%.1f\"", num);
the result is absolutely the same, innit? "-3.4"

and if we have

Code: Select all

double num = 3.4;
System.out.printf("\"%+.1f\"", num);
System.out.println();
System.out.printf("\"%.1f\"", num);
we will have "+3.4" and "3.4"

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

Posted: Wed Aug 12, 2015 7:24 am
by admin
I don't know if you can do that. You will need to check the table mentioned above and see how to avoid printing the - sign.

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

Posted: Wed Aug 12, 2015 7:47 am
by monseigneur
alright, man, thanks for your time.

i don't have troubles hiding negative sign, i just feel that correct definition of the flag would be
b. "+" Include the + sign with this argument if it is positive - Applicable only if conversion char is d or f (i.e. for numbers).
because i cannot think of any example of using "+" flag to include negative sign in the resulting string, negative sign will be there anyway if it is contained in the input.
unless we use "(" flag, of course.
and if we use "(" flag, having "+" flag as well will not change anything, we will still have no negative sign in the resulting string.

but we can leave it here, doesn't really matter.

great job on the software and the site, mate, real helpful!

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

Posted: Wed Aug 12, 2015 8:13 am
by admin
OK, I understand what you mean. You will need to take this up with Oracle though because as per their table, "+ Includes sign, whether positive or negative." :)