Results 1 to 7 of 7
- 10-28-2011, 01:24 AM #1
Member
- Join Date
- Oct 2011
- Posts
- 49
- Rep Power
- 0
Calling methods on objects stored in a vector
I am trying to serialise and deserialise some objects. I have saved about 20 objects in a vector. Now I want to deserialise and show their names, price etc. At first I just decided to read the objects by simply calling on readObject(vector) but I only got
Computer@201f9
Computer@201f9
...
Software@201f9
Software@201f9
...
printed out although I wanted to print out the name of the computer and software, their price and so on. And then I thought maybe I am doing it wrong and decided to use the getter methods to get the names, prices and so on. So I have something like this:
Java Code:public void readItems() { ... Vector<Object> vreader = new Vector<Object>(); vreader = (Vector<Object>) ois.readObject(); Enumeration e = vreader.elements(); while(e.hasMoreElements()) { //here since the vector stored the computer and software objects, I am assuming // that nexElement() goes through the vectors, object by object, and then //I want to call the method getName on each object but I get an error "The method getName() //is undefined for the type Object System.out.println(e.nextElement().getName()); } }Last edited by Dreaming; 10-29-2011 at 10:40 PM.
- 10-28-2011, 01:26 AM #2
Re: Calling methods on objects stored in an array
Do your classes have toString methods. If not they inherit toString from Object and the output you posted looks exactly what you get from Object.toString method.
- 10-29-2011, 10:08 PM #3
Member
- Join Date
- Oct 2011
- Posts
- 49
- Rep Power
- 0
Re: Calling methods on objects stored in an array
Yeah you are right. I have a tostring class but I don't get why I get Computer@17310f etc. instead of what I want, namely printing out properties like the name, price of the computer and software objects etc. I just get things like [Computer@173a10f, Computer@173a10f, Software@32c41a, Software@32c41a] printed out. I have saved the computer and objects in a vector and want t print out their properties like their names, prices and so on.
-
Re: Calling methods on objects stored in an array
No, not a "tostring class" but a toString method. Each class where you want to show the information held in the class should have a public toString() method that returns a String. Make it return a String that shows the name, price, etc... whatever is relevant to defining the object.
For instance, one of my projects looks like so:
Note the toString method which has an @Override annotation to make sure that the method does in fact override the default toString method of Object.Java Code:public class BillingCode { private String text; private String code; public BillingCode(String text, String code) { this.text = text; this.code = code; } public String getText() { return text; } public String getCode() { return code; } @Override public String toString() { return text + " (" + code + ")"; } }
- 10-29-2011, 10:35 PM #5
Member
- Join Date
- Oct 2011
- Posts
- 49
- Rep Power
- 0
Re: Calling methods on objects stored in an array
Sorry, that was meant to read toString method, not class.
I am supposed to write a fairly complicated and quite different number of classes. Basically, I have an abstract Item class, then under it classes Computer and Software (which inherit from the abstract class). And then there are other classes ComputerDAO, SoftwareDAO, ItemManagement and TestArticle, so it is quite complicated (for a complete beginner at least.) In the instruction, it said, we should implement the toString method in the computer and software classes, which I did this way (for the class Software):
And we are also supposed to write the objects to a file, and read from it, something that I (hope) did with serialisation. And finally we are supposed to create a single instant of the class ArticleManagement in the TestArticle that has the main method, which should implement all the functionality of the class. I was only asking this part because it is where I have problem with currently, just taking it one step by step.Java Code:public String tostring() { return new String (getName()+ getType()+ getVersion()+ getStartingprice()+ getAllbids()+ branch); } }
When I ran a test by creating a simple class Person with names, ages, profession and so on, and then implemented the toString, it works without problems, although when I use an array, the toString method prints out something different.Last edited by Dreaming; 10-29-2011 at 10:38 PM.
-
Re: Calling methods on objects stored in an array
For an array, you'll need to iterate through the array in the toString method, perhaps collecting the information it holds in a StringBuilder object and then return the String returned from StringBuilder's toString(). Another option is to use java.util.Arrays.toString(myArray). Always use the @Override annotation so that the compiler will tell you if you have spelled, cApItAlIzEd and configured things correctly (for instance, your "tostring()" method would be caught by this).
- 10-30-2011, 12:50 AM #7
Member
- Join Date
- Oct 2011
- Posts
- 49
- Rep Power
- 0
Re: Calling methods on objects stored in an array
Thanks but I ma still unable to print out what I want. I will post an excerpt from the code in the main
The methods to read and write the files are these, just in case something maybe wrong with them, they are in another class; here is the one from the class SoftwareDAO.Java Code:public class TestItem{ public static void main (String[] args){ ItemManagement im = new ItemManagement(); Vector <Object> vector_Itemn = new Vector<Object>(); Vector <Object> vector_comp = new Vector<Object>(); Vector <Object> vector_softw = new Vector<Object>(); double[] bids = {23.4, 56.7, 67.8}; Software s = new Software ("Whatever", "Dunno", 1.2, 54.0, bids, "Intel"); for(int i = 0; i < 10; i++) vector_softw.add(s); double[] bid = {19.4, 236.7, 867.8}; String[] specs = {"4 GB", "Dual core"}; Computer c = new Computer ("Lenovo", "Dunno", 1.2, 540, bid, "Intel", "Whatevr", specs); for(int i = 0; i < 10; i++) vector_comp.add(c); vector_Items.addAll(vector_comp); vector_Items.addAll(vector_softw); im.saveItems(vector_Items); im.getItems() //This is the key line. The getItems is supposed to print out the items written into the files by saveItems(vector_items) but // I don't get the items printed out System.out.println(am.getItems()); }
Another question, how can I call object methods on elements of the vector. I mean the elements of vectors are objects that possess certain methods. So I want to know why I get an error, if say, I write vector.elementAt(index).methodofheobjectinthisvect or. To me, since the vector elements are objects, I should be able to use the methods of the objects on these elements, since they are in essence just objects, or am I getting it wrong?Java Code:public void saveItems(Vector<Object> v_softw) { Vector<Object> vector_softw = v_softw; try { //the f_name is the name of the file, which is a String member of the class. ObjectOutputStream oos = new ObjectOutputStream( new FileOutputStream( f_name)); oos.writeObject(vector_softw); oos.close(); } catch (IOException e) { e.printStackTrace(); } } public Vector<Object> getItems() { Vector<Object> vreader = new Vector<Object>(); try { ObjectInputStream ois = new ObjectInputStream( new FileInputStream( f_name)); vreader = (Vector<Object>) ois.readObject(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } return vreader; } }
Similar Threads
-
Calling Stored Procedures from oragle10g
By chizbox in forum JDBCReplies: 2Last Post: 02-19-2010, 05:17 AM -
Problem calling Oracle stored procedure (bug?)
By ulix83 in forum JDBCReplies: 3Last Post: 10-14-2009, 09:32 AM -
calling stored procedure through java
By KTR in forum JDBCReplies: 2Last Post: 01-30-2009, 12:53 PM -
Calling SQL Server stored procedures
By Java Tip in forum Java TipReplies: 0Last Post: 01-07-2008, 08:40 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks