Results 1 to 7 of 7
  1. #1
    Dreaming is offline Member
    Join Date
    Oct 2011
    Posts
    49
    Rep Power
    0

    Default 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.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default 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.

  3. #3
    Dreaming is offline Member
    Join Date
    Oct 2011
    Posts
    49
    Rep Power
    0

    Default 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.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default 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:

    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 + ")";
       }
    }
    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.

  5. #5
    Dreaming is offline Member
    Join Date
    Oct 2011
    Posts
    49
    Rep Power
    0

    Default 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):
    Java Code:
    public String tostring() 
    	{
    		return new String (getName()+ getType()+ getVersion()+ getStartingprice()+ getAllbids()+ branch);
    		}	
    }
    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.

    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.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default 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).

  7. #7
    Dreaming is offline Member
    Join Date
    Oct 2011
    Posts
    49
    Rep Power
    0

    Default 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

    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());
    }
    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 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;
        }			
    }
    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?

Similar Threads

  1. Calling Stored Procedures from oragle10g
    By chizbox in forum JDBC
    Replies: 2
    Last Post: 02-19-2010, 05:17 AM
  2. Replies: 3
    Last Post: 10-14-2009, 09:32 AM
  3. Replies: 2
    Last Post: 01-30-2009, 12:53 PM
  4. Calling SQL Server stored procedures
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-07-2008, 08:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •