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

  • Home
  • SEARCH
  • 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 8735171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:59:27+00:00 2026-06-13T09:59:27+00:00

I am trying to make a program where I can add a name and

  • 0

I am trying to make a program where I can add a name and it’s supposed to be saved in a RandomAccessFile, (alphabetically). Whenever I add a name it gets saved in a file, it’s supposed to have at the end the next corresponding position for the next name. I’m having problems with the saving whenever I add a name with starting with A, then I add a name with C, and if I where to add a name starting with B it’s not pointing me in the right alphabetical order.

Here is what an example of what the program should do:

I add a name starting with A.

Numbers on “left” side are just where the next name start,
Numbers on “right” side are the pointers to the next name

[0]—–A—-[-1] ———– the “-1” pointer means its the end of the list

I add a name starting with C.

[0]—–A—-[100] ——- the “100” pointer means that the next name “C” start on byte 100

[100]—C—-[-1] ——— end of the list pointer, notice how A no longer has the “-1” pointer

I add a name starting with B.

[0]—–A—-[200] —— “A” no longer points to 100 because the next letter should be “B”

[100]—C—-[-1] ——– -1 still means that “C” is the end of the list pointer

[200]—B—-[100] ——— “B” is pointing at “C” because the next letter after

Here is my code so far, but I’m missing the part where a name that belongs in the middle of the list is added.

public boolean add(String name, String lastName, String telf) {

    try {
        fileSize = file.length();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    if (fileSize == 0) { //must be a new entry

        try {

            byte entry[] = new byte[sizeEntry];  // size of each entry
            file.write(entry);
            file.seek(file.getFilePointer() - sizeEntry);

            file.writeUTF(name);   //name gets saved
            file.writeUTF(lastName);// last name gets saved
            file.writeUTF(telf);    // telf gets saved
            file.writeUTF("N");     // deleted "Y" or "N" gets saved
            file.writeUTF("-1");    // pointer gets saved

        } catch (IOException e) {
            System.out.println("Error at saving....");
            e.printStackTrace();
        }

    } else {
        pPresent= 0;     //variable for the pointer reading
        pPrevious= 0; // variable for the pointer read

        try {
            file.seek(0); //start reading at the top
            do {
                pPresent= file.getFilePointer();//saves present pointer
                file.seek(pPresent);//seeks to present pointer
                nameRead = file.readUTF();  //reads name
                file.readUTF();  //reads last name
                file.readUTF();  //reads telf
                file.readUTF();  //reads deleted?
                pNext= Long.parseLong(file.readUTF()); // reads the next pointer

                int comparison= name.compareTo(nameRead);
                if (comparison< 0) {

                    //enters here if the new entry goes before the present entry
                    if (pNext!= -1) {
                        file.seek(pNext);//enters here if pointer is not at end of list
                    } else {
                        try {// proceeds to writing a new entry 

                            file.seek(file.length()); //goes to the end of the file
                            byte entry[] = new byte[sizeEntry];
                            file.write(entry);
                            file.seek(file.getFilePointer() - sizeEntry);

                            file.writeUTF(name);
                            file.writeUTF(lastname);
                            file.writeUTF(telf);
                            file.writeUTF("N");
                            file.writeUTF(Long.toString(pPrevious));//writes the previous pointer

                            file.seek(pPrevious);//seeks to the previous entry
                            file.readUTF();//reads name
                            file.readUTF();//reads lastname
                            file.readUTF();//reads telf
                            file.readUTF();//reads deleted?
                            file.writeUTF(Long.toString(pPrevious));//overwrites the new previous

                        } catch (IOException e) {
                            System.out.println("Error at saving...");
                            e.printStackTrace();
                        }
                        break;//exits
                    }
                } else {//enteres here if the entry is bigger than the present

                    if (pNext!= -1) {
                        file.seek(pNext);
                    } else {
                        try {

                            pPresent= file.length()-sizeEntry;//saves present entry
                            file.seek(pPrevious); //seeks to previous entry
                            file.readUTF();//reads name
                            file.readUTF();//reads last name
                            file.readUTF();//reads telf
                            file.readUTF();//reads deleted
                            file.writeUTF(Long.toString(pPresent+100));//overwrites the next pointer

                            file.seek(file.length());//seeks at the end
                            byte entry[] = new byte[sizeEntry];//creates a new entry
                            file.write(entry);
                            file.seek(file.getFilePointer() - sizeEntry);

                            file.writeUTF(name);//writes name
                            file.writeUTF(lastname);//writes lastname
                            file.writeUTF(telf);//writes telf
                            file.writeUTF("N");//writes deleted
                            file.writeUTF(Long.toString(pNext));//writes next pointer

                        } catch (IOException e) {
                            System.out.println("Error at saving...");
                            e.printStackTrace();
                        }
                        break;//exits
                    }
                }
                pPresent= file.getFilePointer();//present pointer read
                pPrevious= pPresent;//present pointer becomes previous
            } while (true);


        } catch (IOException e) {

            System.out.println("Error at saving....");
            e.printStackTrace();
        }
    }
    return false;
}

I hope you guys understand the idea of the program a little better now with the source code. The part I don’t know how to do is where I add a entry that belongs in the middle of the list. Remember that the order of the names dont change only the pointers pointing to the next do.

  • 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-13T09:59:29+00:00Added an answer on June 13, 2026 at 9:59 am

    Finding the insertion point will require traversing the list, which in turn requires a disk access for every single name. Assuming you have 1 million names, and a typical disk access time of 10 ms, inserting a single name would take about 3 hours. Put differently, linked lists are an extremly unsuitable data structure for storing data on disk.

    A reasonable data structure such as a B-Tree would permit lookup and insertion among 1 million names in as little as 3 disk accesses.

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

Sidebar

Related Questions

I am trying to make a program that can take an .mp3 file from
I'm trying to make a program that can be hosted by many peoples, like
Ok, so I am trying to make a program that can find the time
I'm trying to make a program that creates and reads a binary file, which
I am trying to make a program about recipes. The user would have to
I'm trying to make a program that read a file line by line and
Im trying to make a program that retrieves an endless amount of numbers that
I'm trying to make a program that, using pointers, detects lines in an image
I'm trying to make a program that lets you input any one-step equation, and
I'm trying to make a program which will create a sum of directories 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.