I have made a singly linked list with node objects which contain a data variable and a next node variable. a Customer object is stored in the data variable the customer file has a surname variable and a forename variable as follows:
public customer
{
String Surname;
String Forename;
get and set methods for each;
}
The Linked list and node class will sort in ascending order by surname (A-Z) so that George Clooney precedes Jay Gatsby. The methods used to insert an item in the linked list recursively are fine and I have run numerous tests before even trying to save the file using ObjectOutputStream and FileOutputStream or load the file using ObjectInputStream and FileInputStream.
As a result of all the research I have done, I have concluded that it is better to save the whole linked list as an object using the serializable interface on my custom linked list (not a standard Java list) to serialize it and save it to the file.
Now, here’s my problem: I add 1 Node object with a random surname and it works fine: each time the item is added the list is deserialized from the file and loaded into a new SinglyLinkedList object. The new node is added and placed at the right position in the list and then this new list is passed back to the file with ObjectOutputStream and FileOutputStream.
I add one item and it is fine; however, if I add another, it says that it is fine (no errors – I have all the appropriate try and catch statements), but it doesn’t actually update the list. The print method reinforces this, as it also accesses the list in the same fashion and only prints the first item (even if more are added). When I ask for the length of the list, it simply gives me the first item (e.g. returns 1).
If I were to close it and re-run it, I would once again receive this same 1 Node and nothing more. Then, if I were to add another, it would not work once again.
Here are some useful bits of info:
- The customer file, node and singly linked list classes have the necessary serializable imports and implements Serializable headers
- There are 4 separate classes, the customer file-contains the data, the Node-contains the customer file and the next node, the Singly Linked List-contains all methods for editing a list and the Main class which contains all the Scanners and Serializable Stream objects (e.g. ObjectOutputStream) and takes commands and data from the keyboard (e.g. “ADD”, “PRINT”)
- There are separate methods for saving and loading to the file which are used to reduce code coverage on screen- this may be the problem – I am a novice with this but here is the code.
Code for adding a node in MAIN METHOD – not recursive routine
CustomerFile custDat = new CustomerFile(fName,sName);
Node custNode = new Node(custDat, null);
SinglyLinkedList a = loadListFromFile();
if (a == null)
{
System.out.println("Creating new list");
SinglyLinkedList newList = new SinglyLinkedList();
newList.addRecord(custNode, null);
a = newList;
}
else
{
a.addRecord(a.getHead(), custNode);
}
saveListToFile(a);
System.out.println("File added successfully");
and the loadListFromFile method (the save list method is similar, but with output instead) has a set fileName:
private static SinglyLinkedList loadListFromFile()
{
SinglyLinkedList lst = null;
try
{
ObjectInputStream is = new ObjectInputStream(new FileInputStream(fileName));
lst = (SinglyLinkedList) is.readObject();
is.close();
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
return lst;
}
fortunately, I have solved the problem! thank you for your help but the problem was down to a failure in my logic. I running a loop that took values and added them to nodes, these nodes were being added to a list which was being declared NEW INSIDE THE LIST – ie the list could not reach the saveListToFile method and hence would not add to the list being saved in the file