Re: How to cast an Object to Double?
- From: Christian <fakemail@xxxxxx>
- Date: Wed, 17 Oct 2007 20:29:09 +0200
www schrieb:
Mark Rafn wrote:
you're getting a runtime
error, and it is Exception in thread "main"Thank you so much for your reply. I still don't understand:
java.lang.ClassCastException: java.lang.String
at Foo.main(Foo.java:28)
This means you're trying to cast a String into a Double. That won't
work.
Casting is different than parsing. Casting is just a way to tell one
part of
the code that you, the developer, have more information about the type
of the
object, and you want to try to use the object as if it were this more
specific
type. If it's NOT actually usable as that type, it'll get a
ClassCastException.
A String isn't a Double. It's a String. So the cast fails.
<java>
//copy entries from states file to the map
for (Map.Entry value : states.entrySet())
{
map.put((String)value.getKey(), value.getValue()); //I
thought it is String-Object pair!!!
}
<java>
Then, you are saying:
map.get("HAT_SIZE") is a String, that is why (Double)map.get("HAT_SIZE")
will fail. (A String cannot be casted to a Double).
I thought map.get("HAT_SIZE") is an Object, due to the map Generics
definition. If so, (Double)map.get("HAT_SIZE") should work.
I think:
map.put((String)value.getKey(), value.getValue());
is the one causing all the trouble and confusion. The values in the map
are a bunch of String, not Object. But why they are not Object?
value.getValue() is Object.
The map definition(Map<String, Object> map = new HashMap<String,
Object>(10)) also show that the value is Object type.
the problem ist not that they are not Object the problem is that they
are not Double..
you may only cast to Double what is of instance Double (means is Double
or extends double)
you can cast an animal to cat if it is a cat but you can't cast an
animal to dog if it is a cat, although it is also an animal..
.
- References:
- How to cast an Object to Double?
- From: www
- Re: How to cast an Object to Double?
- From: www
- Re: How to cast an Object to Double?
- From: Mark Rafn
- Re: How to cast an Object to Double?
- From: www
- How to cast an Object to Double?
- Prev by Date: How to protect directory in JBoss/Jaas?
- Next by Date: desktop.open not working, no exceptions or errors
- Previous by thread: Re: How to cast an Object to Double?
- Next by thread: Re: How to cast an Object to Double?
- Index(es):
Relevant Pages
|