Results 1 to 5 of 5
  1. #1
    lannie1980 is offline Member
    Join Date
    Mar 2012
    Posts
    88
    Rep Power
    0

    Default Help with writing list to file as serialised objects ;)

    Hey folks,

    I'm having trouble with this assignment question so any pointers is much appreciated.

    I need to search the list for occurrences of a string and then write only these entries in the list to a file.

    This is my code
    XML Code:
    public void writeFemaleCompetitorsFile()
    		 {
    		    String pathname = OUFileChooser.getFilename();
    		    File aFile = new File(pathname);
    		    ObjectOutputStream OutputStream = null;
    		    try
    		    {
    		       OutputStream = new ObjectOutputStream(new FileOutputStream(aFile));
    		       this.sortCompetitorList();
    		       for(Competitor eachCompetitor : competitors)
    		       {
    		          boolean found = competitors.contains("female");
    		          if(found)
    		          {
    		             OutputStream.writeObject(eachCompetitor);
    		             System.out.println(eachCompetitor);
    		          }
    		       }
    		    }
    		    catch(Exception anException)
    		    {
    		       System.out.println("Error: " + anException);
    		    }
    		    finally
    		    {
    		       try
    		       {
    		          OutputStream.close();
    		       }
    		       catch(Exception anException)
    		       {
    		          System.out.println("Error: " + anException);
    		       }
    		    }
    		 }
    }
    It's the test for the existence of the string that I'm stuck at and then choosing only those entries that have this string. Thanks in advance;)

    Regards

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: Help with writing list to file as serialised objects ;)

    the test for the existence of the string that I'm stuck at
    Java Code:
    boolean found = competitors.contains("female");
    Are you asking how the contains() method works?
    What is the definition of the competitors variable? What does the API doc say for its contains() method?

    Can the competitors object contain Strings? "female" is a String. It looks like it contains Competitor objects.

    If the Competitor class has a String member variable that you want to test the value of, you will need to add a method to the class to do that.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    lannie1980 is offline Member
    Join Date
    Mar 2012
    Posts
    88
    Rep Power
    0

    Default Re: Help with writing list to file as serialised objects ;)

    Hi Norm and cheers again.

    I tried Collections.binarySearch(competitors, "female") but this didn't work....

    The competitors variable is actually a list. This list contains Competitor objects - these objects have a String attribute 'sex'. I need to iterate through the list and search for the String "female" and then only process these entries for the serialisable file to be written. I only want the list entries that have "female" in them to be added to the new file.

    The question requirements don't allow for another method to be made - it must be done within this method.

    Regards

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: Help with writing list to file as serialised objects ;)

    search for the String "female"
    Where is the String: "female"? If the String is inside of a Competitor object, then the Competitor class must have a method that retrieves that String from inside of the object. What if the Competitor object has more than one String members?
    Does the list have any String objects in it? Inside of other objects does not count.
    Did the code every do anything like this: list.add("female");
    If it did, the list will contain the String: "female".
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    lannie1980 is offline Member
    Join Date
    Mar 2012
    Posts
    88
    Rep Power
    0

    Default Re: Help with writing list to file as serialised objects ;)

    Hi,

    ok I finally managed to get it working - it was to do with the method used to retrieve the String - I do not know what the hell I was trying to do with the boolean found ;(

    Thanks again Norm.

    Regards

Similar Threads

  1. Error in writing JButton Array of objects into File
    By justbeller in forum AWT / Swing
    Replies: 3
    Last Post: 06-09-2011, 12:06 AM
  2. Replies: 1
    Last Post: 02-27-2010, 02:29 AM
  3. add objects to list
    By javaMike in forum Advanced Java
    Replies: 1
    Last Post: 11-17-2009, 11:11 PM
  4. Replies: 1
    Last Post: 01-22-2009, 05:25 PM
  5. Writing Data or Objects?
    By Goodwine in forum New To Java
    Replies: 2
    Last Post: 11-26-2008, 01:36 AM

Tags for this Thread

Posting Permissions

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