java - Linked List not reading file correctly -
so have linked list class has of constructor data necessary read file have , convert linked list. when reads in file data adding lines , spaces incorrectly. using system.out.println check , coming out incorrectly. don't think tostring method because have messed , nothing changes. need because cant figure out. thanks!
file information(basically of data on separate lines):
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
linked list:
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; } }
read file , create linked list method:
linkedlist<linkedaccountlist> account = new linkedlist<linkedaccountlist>(); try { read(account, "file.txt"); } catch (exception e) { system.err.println(e.tostring()); } display(account); } public static void read(linkedlist<linkedaccountlist> 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(); }
here output looks like(look closely , can see problem):
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
you don't have validation, logging. should @ least create method:
public string readline(bufferedreader br){ string rl = br.readline(); if (rl.trim().length() > 2) { return rl; }else return readline(br); }
and use like:
... while(infile.ready()) { string username = readline(infile); // instead of string username = infile.readline();
also using long constructor argument error-prone
linkedaccountlist (string username, string password, string email, string name, string breed, string gender, string age, string state, string hobby){
use ony setx(string arg)
method. common practise in dao pattern.
Comments
Post a Comment