Note that there are some explanatory texts on larger screens.

plurals
  1. POLinked List not reading file correctly
    primarykey
    data
    text
    <p>So basically I have a Linked List class that has all of the constructor data necessary to read a file I have and then convert it into a Linked List. However when it reads in the file data it is adding lines and spaces incorrectly. I am using system.out.println to check and it is coming out incorrectly. I don't think it is the toString method because I have messed with it so much and nothing changes. I need help with this because I cant figure it out. Thanks!</p> <p><strong>File Information(Basically All of the data is on separate lines):</strong></p> <pre><code>tobi tobi tobi@hotmail.com tobi Mixed Breed Male 3-4 Virginia Walking lily lily lily@hotmail.com lily Yorkshire Terrier Female 3-4 Hawaii Jumping peppy peppy peppy@hotmail.com peppy Chihuahua Male 7-8 Alaska Sleeping fluffy fluffy fluffy@hotmail.com fluffy MixedBreed Female 3-4 Virginia Walking flower flower flower@hotmail.com flower Chihuahua Female 7-8 Alaska Sleeping </code></pre> <p><strong>Linked List:</strong></p> <pre><code> import java.io.Serializable; import java.util.*; public class LinkedAccountList implements Serializable{ private String username; private String password; private String email; private String name; private String breed; private String gender; private String age; private String state; private String hobby; public LinkedAccountList (String username, String password, String email, String name, String breed, String gender, String age, String state, String hobby){ this.username = username; this.password = password; this.email = email; this.name = name; this.breed = breed; this.gender = gender; this.age = age; this.state = state; this.hobby = hobby; } public String getUsername(){ return username; } public String getPassword(){ return password; } public String getEmail(){ return email; } public String getName(){ return name; } public String getBreed(){ return breed; } public String getGender(){ return gender; } public String getAge(){ return age; } public String getState(){ return state; } public String getHobby(){ return hobby; } @Override public String toString() { return "Username: "+username+"\nPassword: "+password+"\nEmail: "+email+"\nName: "+name+"\nBreed: "+breed+"\nGender: "+gender+"\nAge: "+age+"\nState: "+state+"\nHobby: "+hobby; } public void setUsername(String u){ username = u; } public void setPassword(String p){ password = p; } public void setEmail(String e){ email = e; } public void setName(String n){ name = n; } public void setBreed(String b){ breed = b; } public void setGender(String g){ gender = g; } public void setAge(String a){ age = a; } public void setState(String s){ state = s; } public void setHobby(String h){ hobby = h; } } </code></pre> <p><strong>Read from File and create Linked List Method:</strong></p> <pre><code> LinkedList&lt;LinkedAccountList&gt; account = new LinkedList&lt;LinkedAccountList&gt;(); try { read(account, "file.txt"); } catch (Exception e) { System.err.println(e.toString()); } display(account); } public static void read(LinkedList&lt;LinkedAccountList&gt; account, String inputFileName) throws java.io.IOException { BufferedReader infile = new BufferedReader(new FileReader(inputFileName)); while(infile.ready()) { String username = infile.readLine(); String password = infile.readLine(); String email = infile.readLine(); String name = infile.readLine(); String breed = infile.readLine(); String gender = infile.readLine(); String age = infile.readLine(); String state = infile.readLine(); String hobby = infile.readLine(); LinkedAccountList d = new LinkedAccountList(username, password, email, name, breed, gender, age, state, hobby); account.add(d); } infile.close(); } </code></pre> <p><strong>Here is what the output looks like(Look closely and you can see the problem):</strong></p> <pre><code>Username: tobi Password: tobi Email: tobi@hotmail.com Name: tobi Breed: Mixed Breed Gender: Male Age: 3-4 State: Virginia Hobby: Walking Username: Password: lily Email: lily Name: lily@hotmail.com Breed: lily Gender: Yorkshire Terrier Age: Female State: 3-4 Hobby: Hawaii Username: Jumping Password: peppy Email: peppy Name: peppy@hotmail.com Breed: peppy Gender: Chihuahua Age: Male State: 7-8 Hobby: Alaska Username: Sleeping Password: fluffy Email: fluffy Name: fluffy@hotmail.com Breed: fluffy Gender: Mixed Breed Age: Female State: 3-4 Hobby: Virginia Username: Walking Password: flower Email: flower Name: flower@hotmail.com Breed: flower Gender: Chihuahua Age: Female State: 7-8 Hobby: Alaska Username: Sleeping Password: null Email: null Name: null Breed: null Gender: null Age: null State: null Hobby: null </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload