Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8995515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:37:50+00:00 2026-06-15T23:37:50+00:00

I have made a singly linked list with node objects which contain a data

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:37:50+00:00Added an answer on June 15, 2026 at 11:37 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with my code, I have made a singly linked list
I have made my own single chained/linked list. Now, if I want to delete/remove
I have made an application which calls the phone's contact list but i want
I have made a code that read data from flash Nand (without filesystem). fd
I have made an iphone application like flip cards in which values of cards
I have made my own exception class which derives from runtime_error and is getting
I hope I am using the right terminology. I have made a single-chained list.
I have made an OS simulator for a project, for the next part I
I have made an each statement which iterates through each class item found. At
I am displaying the list of objects in the html table. i have the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.