wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes converting object[] to double? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "converting object[] to double?" Watch "converting object[] to double?" New topic
Author

converting object[] to double?

sahar sa
Ranch Hand

Joined: Jul 06, 2009
Posts: 102
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.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

What error do you see?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
sahar sa
Ranch Hand

Joined: Jul 06, 2009
Posts: 102
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.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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();


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: converting object[] to double?