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
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
Re: Help with writing list to file as serialised objects ;)
Quote:
the test for the existence of the string that I'm stuck at
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.
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
Re: Help with writing list to file as serialised objects ;)
Quote:
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".
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