File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Problem with the Wrapper Conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem with the Wrapper Conversion" Watch "Problem with the Wrapper Conversion" New topic
Author

Problem with the Wrapper Conversion

Faisal Fuad
Ranch Hand

Joined: Jul 05, 2011
Posts: 79

Guys...I was reading the Wapper Conversion rules and was trying to do the following, which is now giving me NumberFormatException. May be the basic part for Wrapper Class I am missing here to understand correctly. Can any one please help me to be on the right side?


OUTPUT:


Best wishes
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
You are trying to create an Integer with a double value. Similar to if you had tried
Faisal Fuad
Ranch Hand

Joined: Jul 05, 2011
Posts: 79

But why that is not Widening the value for me? Isn't it an automatic process in Java? Or what i was trying to do is illegal in Java ! I know that Wrapper's second big function is convering stuff....so why can't i do that here?

Can you please show me a good example on converting a String to a Wrapper?

Thanks
Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

Yes, its illegal in Java, and you can check yourself.

see if you're able to compile above code?


Saifuddin..
[Blog][Linkedin] How To Ask Questions On JavaRanch My OpenSource
Faisal Fuad
Ranch Hand

Joined: Jul 05, 2011
Posts: 79

Thanks both Dennis and Muhammad...yes...certainly i can't compile that above code....but here trying to under stand how particularly Wrapper can help me, though it's second best function is to conver.
Faisal Fuad
Ranch Hand

Joined: Jul 05, 2011
Posts: 79

Ok...let me share this...the following is clear:


Is there any Good example for Wrapped String to Primitive? (In this case Integer)
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Faisal:

Strings aren't wrapped. If you look at the JavaDoc for Integer.valueOf(String s), you'll see that it's quite clear that if the String doesn't represent an integer value, you'll see the NumberFormatException you got. If you want to convert a String representing a floating point number, you'll have to add an intermediate step.

John.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40034
    
  28
Faisal Fuad wrote:But why that is not Widening the value for me?. . .
I know it’s late, but that is not widening. It’s narrowing. More details in the Java Language Specification.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with the Wrapper Conversion