• 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
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

thread question

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public boolean strCrt(String s){
try{
factor = Float.valueOf(s).floatValue();
return true;
}
catch(NumberFormatException e){
System.out.println("Bad number " + s);
factor = Float.NaN;
}
finally{
System.out.println("Finally");
}
return false;
}
Input ="1.2"-Result:factor = 1.2, true is returned, nothing is printed.
Input = ""-Result:factor unchanged, Finally is printed, false is returned
Input = ""-Result:factor=NaN,"Bad number finally is printed,falsed is returned
ans is C
I really don't understand this problem, anyone explain to me?

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
valueOf() method throws a NumberFormatException
When caught the output =Bad Number+""
Then ur assigning factor=Float.NaN
Then finally block is executed hence the output =Finally
Then false is returned
Hence the final output
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic