• 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

converting object[] to double?

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear,
I have a method called (getContentObject) which returns me an Object[]. I have a piece of code like this:

result = (Object[]) reply.getContentObject();
double price = Double.parseDouble((String)result[2]);

actually in the last line I've tried to convert object to double but I cant. can you tell me how can I make it ?

Thanks,
Sahar.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What error do you see?
 
sahar sa
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear
First, when I run, it throws:
*** Uncaught Exception for agent BookBuyerAgent0 ***
java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.String

I guess, because a double has been set to the second place of Object Array, and now I can not convert it to string?!.
Actually what I want to do is just to convert an element of object array to a double value! thats all!

second, sorry for double posting, was a mistake. I've tried but I didnt know how to delete.

thank you again.
Sahar.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the array contains a Double, then you don't have to convert it to a String and parse it, you can just use autoboxing:

double price = (Double) result[2];

The compiler will actually emit code as if you had typed

double price = ((Double) result[2]).doubleValue();
 
Brace yourself while corporate america tries to sell us its things. Some day they will chill and use tiny ads.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic