Patricia Klimek wrote:
> the Problem I have is kind of crazy:
> I want to cast a vector to Integer and save elementAt(1) in Integer i:
You cannot cast a Vector to Integer. It isn't one. If some element of
the vector is an Integer then you can cast that; that is what your first
line of code below attempts to do:
> Integer i = (Integer)row.elementAt(1);
The method invocation operator (.) has precedence over the typecast, so
it happens first and you are attempting to cast the return value from
type Object to type Integer. That will succeed of the element is an
Integer or if it is null.
> It does show me the Element if I do this:
> System.out.println(row.elementAt(1));
> but if I cast it, it's not working.
The only reason a cast ever fails is that the class of the object being
cast is incompatible with the type it is being cast to. In that case a
ClassCastException is thrown. If you are not getting a
ClassCastException then the cast is not your problem; if you _are_
getting one then the problem is that the object you are trying to cast
is not an Integer.
Do make sure you understand that Java typecasts do not *change* the
class of an object -- they simply declare it to be a certain type (or a
subtype) in a way that can be checked at runtime. That means, for
instance, that no String can be successfully cast to Integer, even one
that contains a representation of a number (e.g. "42"). For that
particular case you need to create an Integer by means of
Integer.valueOf(String), or possibly to obtain an int (primitive) value
by means of Integer.parseInt(String).
John Bollinger