Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Something is wrong with "Casting"

Reply
Thread Tools

Something is wrong with "Casting"

 
 
Patricia Klimek
Guest
Posts: n/a
 
      11-30-2004
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
 
Reply With Quote
 
 
 
 
KiLVaiDeN
Guest
Posts: n/a
 
      11-30-2004

"Patricia Klimek" <> wrote in message
news: om...
> 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


 
Reply With Quote
 
 
 
 
Murray
Guest
Posts: n/a
 
      11-30-2004

"KiLVaiDeN" <> wrote in message
news:41ac5528$0$15070$...>
> 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.


 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      11-30-2004
On 30 Nov 2004 03:03:05 -0800, Patricia Klimek wrote:

> System.out.println(row.elementAt(1)); --- is working
> Integer id = (Integer)row.elementAt(1); --- is not working


Does 'is not working' mean the code has knocked off for the day and
gone to the pub for a beer? Does it mean the code is simply lazy?
<http://www.physci.org/codes/javafaq.jsp#exact>

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.LensEscapes.com/ Images that escape the mundane
 
Reply With Quote
 
MaSTeR
Guest
Posts: n/a
 
      11-30-2004
"Patricia Klimek" <> wrote in message
news: om...
> 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 ?


 
Reply With Quote
 
John C. Bollinger
Guest
Posts: n/a
 
      11-30-2004
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
XPath query for <?define something="something" ?> Pekka Järvinen XML 2 04-29-2008 08:12 PM
How to find and replace something that is nested inside something else? alainfri@gmail.com Perl Misc 4 05-31-2007 11:50 PM
var Something= new Something() What does it mean ? pamelafluente@libero.it Javascript 9 10-05-2006 02:43 PM
IOS update - something went wrong :-) elmar bschorer Cisco 9 02-01-2005 08:43 PM
umm... something... template(s)... something else... pointer(s)... and such... 0.o yah, I'm hopeless and clueless o.0 C++ 4 10-13-2004 10:34 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57