Re: How to cast an Object to Double?



www schrieb:
Mark Rafn wrote:
you're getting a runtime
error, and it is Exception in thread "main"
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.

Thank you so much for your reply. I still don't understand:

<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..
.



Relevant Pages

  • Re: what is this c# syntax?
    ... You mention casting an array to a string. ... You can't cast an array to a string because one is not an ancestor of the ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Invalid CastException with SqlDBType
    ... Add(string*, SqlDbType, int, string**) ... The DateTime.Now is trying to cast into a int. ... not in Sql set up the column as identity and let it seed itself. ... > update a MS SQL Database with a stored procedure. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Porting up to VC++ 2005
    ... Since you are dealing with strings that contain file names, ... characters, but that's a different question to be handled in a different way. ... done, which should be unnecessary, it should be an LPTSTR cast, which ... It only applies to string and ...
    (microsoft.public.vc.mfc)
  • Re: casting vs. using the "Convert" methods?
    ... When I was talking of casting I was of course only talking about casting ... because it was a conversion and not a cast. ... > cast operator that converted a string to an int. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Huge string arrays in C#
    ... > in addition, string is a reference type, not a value type, so the casting ... There is no cast to assign a string to an object variable, ...
    (microsoft.public.dotnet.framework.performance)