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 7657369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:00:55+00:00 2026-05-31T13:00:55+00:00

I need to save the linked list I created to a file but I

  • 0

I need to save the linked list I created to a file but I want each part of the users account to be its own element. i.e. (username, password, email, name, breed, gender, age, state, hobby). Something is wrong with my code however and each account is its own element. Any help would be great!

Also here is a link to my Account Class which is used to create the Linked List
http://pastebin.com/jnBrcnP1

Linked List looks like this:

tobi
tobi123
tobi@hotmail.com
tobi
Mixed Breed
Male
1-2
Virginia
Walking
peppy
peppy123
peppy@hotmail.com
peppy
Chihuahua
Male
5-6
Virginia
Eating

Saves to file like this:

tobitobi123tobi@hotmail.comtobiMixed BreedMale1-2VirginiaWalking
peppypeppy123peppy@hotmail.compeppyChihuahuaMale5-6VirginiaEating

Code for creating Linked List:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;
public class Main extends javax.swing.JFrame implements ActionListener{

public static String readLine(BufferedReader br) throws IOException {
    String rl = br.readLine();
    if (rl.trim().length() > 2){
        return rl;
    }else return readLine(br);
}

public static void main(String[] args) {

    LinkedList<Account> account = new LinkedList<Account>();

    try
    {
        read(account, "output.txt");
    } catch (Exception e)
    {
        System.err.println(e.toString());
    }
        display(account);
    }

    public static void read(LinkedList<Account> account, String inputFileName) throws java.io.IOException
    {
        BufferedReader infile = new BufferedReader(new FileReader(inputFileName));
        while(infile.ready())
        {

            String username = readLine(infile);
            String password = readLine(infile);
            String email = readLine(infile);
            String name = readLine(infile);
            String breed = readLine(infile);
            String gender = readLine(infile);
            String age = readLine(infile);
            String state = readLine(infile);
            String hobby = readLine(infile);

            Account a = new Account(username, password, email, name, breed, gender, age, state, hobby);
            account.add(a);
            a.showList();
        }
        infile.close();
    }

    public static void display(LinkedList<?> c)
    {
        for (Object e : c)
        {
            System.out.println(e);
        }
    }

Code for Saving Linked List to file:

        String file_name = "output.txt";
        try {

                FileWriter fstream = new FileWriter(file_name);
                BufferedWriter out = new BufferedWriter(fstream);

                ListIterator itr = account.listIterator();
                while (itr.hasNext()) {
                    Account element = (Account) itr.next();
                    out.write("" + element);
                    out.newLine();
                }

                out.close();
                System.out.println("File created successfully.");

        } catch (Exception e) {
        }
  • 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-05-31T13:00:56+00:00Added an answer on May 31, 2026 at 1:00 pm

    This is the problem, in Account:

    public String toString() {
        return ""+username+"\n"+password+"\n"+email+"\n"+name+
               "\n"+breed+"\n"+gender+"\n"+age+"\n"+state+"\n"+hobby;
    }
    

    You’re assuming that \n is the appropriate line ending. My guess is you’re running on Windows, where it would be \r\n. Personally, I think it would be better for your “writing” code not to use toString() at all, but to write out the lines itself – after all, it knows the format it wants to use.

    (Additionally, I would advise against using "" + ... as a way of converting a value into a string…)

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

Sidebar

Related Questions

.txt file need save in SD-card. Save(n + String.valueOf(key) + .txt); private void Save(String
I need to save some classes and data structures to a file. My first
I need to save all .xml file names in a directory to a vector.
I just need a snippet to open and then save a file. It's an
I need to save and load properties of a Class dynamicly, but what is
I want to save basket of liked properties for visitors. The information I need
I need a save way to say: iOS, I want this method to be
I need to save a xml file from sdcard to my android app, in
I need to be able to search a Linked List for certain unique elements
I need to save some data in .xls file. I am trying to use

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.