Separating parts of an object

Thread Solved

Join Date: Apr 2006
Posts: 114
Reputation: KimJack is an unknown quantity at this point 
Solved Threads: 0
KimJack KimJack is offline Offline
Junior Poster

Separating parts of an object

 
0
  #1
Mar 4th, 2008
Hello All,

I am working with an array of objects that contains names and descriptions.
For example:

Car : A blue jetta
Pet : A small dog
.
.
.

My question is since it is an array of objects, how can I extract just the name "Car" or just the description "A blue jetta".

String tokenizer and substrings do not work. Any suggestion?

Thank you
Last edited by KimJack; Mar 4th, 2008 at 11:47 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,663
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 224
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Separating parts of an object

 
0
  #2
Mar 5th, 2008
From what I understood you have declared the array to contain objects, but you insert Strings. Why don't you create a class with two attributes: name and description and insert that in the array. So when you get the object from the array you will have access to each of these values separately with get methods(getName(), getDescription()).
Now, about your problem:
If you have declared the array to be an array of objects then when you get values of the array you get an object. ex:
  1. Object [] array=new Object[5];
  2. array[0]="Name:Description"
the array contains objects, you insert String which is an Object.String is an Object
BUT when you do this:
  1. String s=array[0];
it is wrong because the array returns an object, not a String:Object is NOT a String
and the method substring applys only for Strings not objects, meaning that you should use casting:
  1. String s=(String)array[0];
If the object that is inside the array ia a String then the casting will work, otherwise it will throw an Exception
Last edited by javaAddict; Mar 5th, 2008 at 3:58 am.
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 114
Reputation: KimJack is an unknown quantity at this point 
Solved Threads: 0
KimJack KimJack is offline Offline
Junior Poster

Re: Separating parts of an object

 
0
  #3
Mar 5th, 2008
Thanks JavaAddict
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC