• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JQPlus Question

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ;
}
}
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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".
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic