File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes JQPlus Question 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 "JQPlus Question" Watch "JQPlus Question" New topic
Author

JQPlus Question

Albert Gray
Ranch Hand

Joined: Mar 06, 2001
Posts: 43
I just added the main method with the class name parstIt in order to print. The explanation given is that the 'return' statement in finally block is unreachable, otherwise it will print options 1,2,3(please see the options in the JQPlus applet).
I find that if you comment out any of the 'return' statements at 'try', 'catch' or 'finally' blocks, it will print only "finally". That does not match with JQPlus - I need further clarifications pls.....
Albert
//Question ID :957638871199
//Following is a supposedly roboust method to parse an input for a float....
class parseIt
{
public static void main(String [] parse)
{
new parseIt().parseFloat("0.1");
}

public float parseFloat(String s)
{
float f = 0.0f;
try
{
f = Float.valueOf(s).floatValue();
return f ;
}
catch(NumberFormatException nfe)
{
System.out.println("Invalid input " + s);
f = Float.NaN ;
//return f;
}
finally { System.out.println("finally"); }
return f ;
}
}
Scott Appleton
Ranch Hand

Joined: May 07, 2001
Posts: 195
Albert, the last return statement in the code is not within the finally block but after the whole try-catch-finally block. That's why it's unreachable if you have a return in both the try and the catch blocks (the method will always return without reaching that statement).
As written, it will indeed print only "finally". The only other print statement in the code is within the catch block, which will not execute when you pass the parseFloat() method a parameter which can be interpreted as a valid float (which is the case with "0.1" in your code).
I don't know how this matches with the JQ+ applet.
Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
Hi, this is because when you comment out any one of the returns, the program becomes runnable, and as you know, the finally block will be run regardless, and finally will be printed. And that only finally will get printed because no exception was thrown, and there was nothing to be printed in try block. If you put something in the try block, it should get printed, if no exception was thrown.
[This message has been edited by Cameron Park (edited July 12, 2001).]
Albert Gray
Ranch Hand

Joined: Mar 06, 2001
Posts: 43
Hi Scott/Cameron,
My point is you can keep any one of the three return statements in try, catch or finally blocks and it will print 'finally'. You not necessarily have to comment out return at the finally block.
Secondly, the given answer says that return statement at the finally block is unreachable. Otherwise choices 1,2 and 3 are valid. The three choices are:
1) if inpit "0.1" then it will return 0.1 and print finally.
2) if input "0x.1" then it will return Foalt.NaN and print invalid input 0x.1 and finally.
3) if inpit "1" then it will return 1 and print finally.
Would you please justify this again.........
Albert
Scott Appleton
Ranch Hand

Joined: May 07, 2001
Posts: 195
Albert, again the final return is not within the finally block but after, which is why it is necessary to comment out one of the other return statements.
The answers look correct to me. You have already verified the first one with this program, and you should be able to verify the other 2 similarly. Keep in mind that "return 0.1" does not mean "print out 0.1".
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JQPlus Question