• 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

try catch

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public boolean stringcvt(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;
}

what is the output? can someone explain...
thnx
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My Friend,
I have seen a lot of questions posted by you. Please make an attempt to give your version of the explanation. Tell us what you think is right or where exactly it confuses you. "Apples are red, violets are blue" kind of answers will not get you anywhere.
Ajith
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Compile error on line 1. Unbalanced brackets.

Regds.
- satya
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well Ajith, i know you are right...but i�v been doing the thinking in mind and wanting more to confirm my answer, anyhow i can also write my thougths about the answer that i think is suitable...
regarding the code above, actually it was in the mock exam and i could not get a proper output when i compiled, my arguments about the code
* first line is not balanced with the ) brackets , but when i put them it gives error on the factor 3rd line, that factor is not defined.
* The problem with valueOf(s) in the code is that s is not defined. I tried a simple code:
and printed the output for str for:
String str = String.valueOf(true);
String str2 = String.valueOf(Math.PI);
so as expected valueOf returns the String conversion for the boolean and primitive datatypes. But what abt the statement in the code
factor= Float.valueOf(s).floatValue();
i could not understand it...!?
*while trying
String str3= String.valueOf(new char[] {�a�,��b�};
i am getting error.
Can someone try this example too?
Since i was stuck with the first two lines in my previous code thats why, Ajith i wanted someone to kindly explain on whats going on!
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's look at this line of code
<PRE>
factor= Float.valueOf(s).floatValue();
</PRE>
Float is a wrapper class and it has a static method with the signature public static Float valueOf(String s) throws NumberFormatException
It "attempts to" convert the argument into a Float object. Remember this is the Float object, and NOT float primitive . So, Float.valueOf(s) would give you a Float object assuming the String s can be converted to a float number without any errors.
Because the code snippet given by you is incomplete, I am assuming 'factor' is declared to be of type float . As Float object cannot be directly assigned to a primitive, we will have to use another method defined on the Float wrapper class called floatValue() which returns the value contained in the wrapper class as a primitive.
In essence, the one line of code above is trying to convert the
value of String s to a floating point number with the help of the
Float wrapper class. Obviously this operation might throw some
exceptions especially when the string argument cannot be converted to a floating point number. That's why we have the try-catch safety net.
About your second question, your code is missing paranthesis in
the first line. Also the characters a and b should be enclosed
in single quotes, not the back-quotes that appears in your snippet. Perhaps when you copied the code from the example, some how the quotes got messed up. One way of avoiding such bugs is to use a Java editor which has syntax highlighting, so that you will know what is wrong soon after you paste the code into a new file.
I fixed these two things and it worked like a magic!
<PRE>
class ValueOfTest
{
public static void main ( String s[])
{
String str3= String.valueOf(new char[]{'a','b'});
System.out.println( str3 ) ;
}
}
</PRE>
Hope this helps.
Ajith
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ajith is right. Include a { and also declare the variable fator to be of float type.
But anyway for your convenience I have made the following chages to your code. Now it works
class ValueOfTest
{
public static void main ( String s[])
{
ValueOfTest obj = new ValueOfTest();
System.out.println("\n"+ obj.stringcvt(null)+"\n" ) ;
System.out.println( "\n"+obj.stringcvt("as")+"\n" ) ;
}
public boolean stringcvt(String s)
{
float factor;
try
{
factor=Float.valueOf(s).floatValue();
return true;
}
catch(NumberFormatException e)
{
System.out.println("\nbad number" + s);
factor= Float.NaN;
}
catch(NullPointerException e)
{
System.out.println("\nnull assigned " + s);
}
finally
{
System.out.println("Finally");
}
return false;
}
}
 
reply
    Bookmark Topic Watch Topic
  • New Topic