| | |
Separating parts of an object
Thread Solved |
•
•
Join Date: Apr 2006
Posts: 114
Reputation:
Solved Threads: 0
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
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.
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:
the array contains objects, you insert String which is an Object.String is an Object
BUT when you do this:
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:
If the object that is inside the array ia a String then the casting will work, otherwise it will throw an Exception
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:
Java Syntax (Toggle Plain Text)
Object [] array=new Object[5]; array[0]="Name:Description"
BUT when you do this:
Java Syntax (Toggle Plain Text)
String s=array[0];
and the method substring applys only for Strings not objects, meaning that you should use casting:
Java Syntax (Toggle Plain Text)
String s=(String)array[0];
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
![]() |
Other Threads in the Java Forum
- Previous Thread: Display output to excel
- Next Thread: java project database error
| Thread Tools | Search this Thread |
-xlint add android api applet application applications array arrays automation bank bi binary blackberry bluetooth chat class client code compile compiler component database development digit eclipse equation error event fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui health html hyper ide idea image infinite input int integer j2me java javame javaprojects jetbrains jni jpanel jtable julia learningresources linux list login loop main map method methods mobile myregfun netbeans newbie nonstatic notdisplaying pearl problem program programming project qt recursion scanner screen scrollbar server set sms sort sorting spamblocker sql sqlserver string superclass swing system text-file thread threads tree variablebinding windows xor






