Something is wrong with "Casting"

P

Patricia Klimek

Hey,

the Problem I have is kind of crazy:
I want to cast a vector to Integer and save elementAt(1) in Integer i:

Integer i = (Integer)row.elementAt(1);

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.

System.out.println(row.elementAt(1)); --- is working
Integer id = (Integer)row.elementAt(1); --- is not working
because of the casting
System.out.println("id: " + row.elementAt(1)); --- is not working
Human human = new Human(id); --- is not working


I probably can't cast the Vector element, right?
But how can I do it?

I hope somebody can help me.

Please

Patricia
 
K

KiLVaiDeN

Patricia Klimek said:
Hey,

the Problem I have is kind of crazy:
I want to cast a vector to Integer and save elementAt(1) in Integer i:

Integer i = (Integer)row.elementAt(1);

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.

System.out.println(row.elementAt(1)); --- is working
Integer id = (Integer)row.elementAt(1); --- is not working
because of the casting
System.out.println("id: " + row.elementAt(1)); --- is not working
Human human = new Human(id); --- is not working


I probably can't cast the Vector element, right?
But how can I do it?

I hope somebody can help me.

Please

Patricia

Try Integer id = new Integer((int)row.elementAt(1));

K
 
M

Murray

KiLVaiDeN said:
Try Integer id = new Integer((int)row.elementAt(1));

K

Vectors don't hold primitives, only objects.

To the original poster, when you say "it doesn't work" please be more
specific. What error are you getting? What are you putting in to the Vector?
If it's not an Integer then you will get a ClassCastException. Show us all
relevent code if you would like a useful response rather than a guess.
 
M

MaSTeR

Patricia Klimek said:
Hey,

the Problem I have is kind of crazy:
I want to cast a vector to Integer and save elementAt(1) in Integer i:

Integer i = (Integer)row.elementAt(1);

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.

System.out.println(row.elementAt(1)); --- is working
Integer id = (Integer)row.elementAt(1); --- is not working
because of the casting
System.out.println("id: " + row.elementAt(1)); --- is not working
Human human = new Human(id); --- is not working


I probably can't cast the Vector element, right?
But how can I do it?

I hope somebody can help me.

Please

Patricia

You do know that any collection in Java is zero based ?
 
J

John C. Bollinger

Patricia said:
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
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top