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

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:

Object [] array=new Object[5];
array[0]="Name:Description"

the array contains objects, you insert String which is an Object.String is an Object
BUT when you do this:

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:

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

Thanks JavaAddict

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.