• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with the Wrapper Conversion

 
Faisal Fuad
Ranch Hand
Posts: 83
IBM DB2 Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 808
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are trying to create an Integer with a double value. Similar to if you had tried
 
Faisal Fuad
Ranch Hand
Posts: 83
IBM DB2 Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1324
Android Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, its illegal in Java, and you can check yourself.

see if you're able to compile above code?
 
Faisal Fuad
Ranch Hand
Posts: 83
IBM DB2 Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
IBM DB2 Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 600
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47222
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic