2012-03-07 73 views
0

所以基本上我有一個鏈接列表類,它具有讀取文件所需的所有構造函數數據,然後將其轉換爲鏈接列表。但是,當它讀入文件數據時,它會錯誤地添加行和空格。我正在使用system.out.println來檢查它是否出錯。我不認爲這是toString方法,因爲我已經搞砸了,沒有什麼改變。我需要幫助,因爲我無法弄清楚。 謝謝!鏈接列表沒有正確讀取文件

文件信息(基本上,所有數據是在不同的行):

tobi 
tobi 
[email protected] 
tobi 
Mixed Breed 
Male 
3-4 
Virginia 
Walking 
lily 
lily 
[email protected] 
lily 
Yorkshire Terrier 
Female 
3-4 
Hawaii 
Jumping 
peppy 
peppy 
[email protected] 
peppy 
Chihuahua 
Male 
7-8 
Alaska 
Sleeping 
fluffy 
fluffy 
[email protected] 
fluffy 
MixedBreed 
Female 
3-4 
Virginia 
Walking 
flower 
flower 
[email protected] 
flower 
Chihuahua 
Female 
7-8 
Alaska 
Sleeping 

鏈接列表:從文件讀取

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; 
    } 

    } 

和創建鏈接列表方法:

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(); 
    } 

這裏是輸出的樣子(仔細看,你可以看到這個問題):

Username: tobi 
Password: tobi 
Email: [email protected] 
Name: tobi 
Breed: Mixed Breed 
Gender: Male 
Age: 3-4 
State: Virginia 
Hobby: Walking 
Username: 
Password: lily 
Email: lily 
Name: [email protected] 
Breed: lily 
Gender: Yorkshire Terrier 
Age: Female 
State: 3-4 
Hobby: Hawaii 
Username: Jumping 
Password: peppy 
Email: peppy 
Name: [email protected] 
Breed: peppy 
Gender: Chihuahua 
Age: Male 
State: 7-8 
Hobby: Alaska 
Username: Sleeping 
Password: fluffy 
Email: fluffy 
Name: [email protected] 
Breed: fluffy 
Gender: Mixed Breed 
Age: Female 
State: 3-4 
Hobby: Virginia 
Username: Walking 
Password: flower 
Email: flower 
Name: [email protected] 
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 
+0

這不是你的問題的答案,但爲什麼你的類叫'LinkedAccountList'而不是'Account'? – Taymon 2012-03-07 01:16:22

+0

你的顯示方式在哪裏? – 2012-03-07 01:19:13

+0

我打算改變它大聲笑,但沒有理由 – 2012-03-07 01:19:55

回答

0

您沒有任何驗證,任何日誌記錄。 你至少應該創建方法:

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

,並用它喜歡:

... 
    while(infile.ready()) 
     { 
      String username = readLine(infile); // instead of String username = infile.readLine(); 

而且使用這麼久構造函數的參數是容易出錯的

LinkedAccountList(字符串username,字符串密碼,字符串電子郵件, 字符串名稱,字符串品種,字符串性別,字符串年齡,字符串狀態, 字符串愛好){

使用ony setX(String arg)方法。這在DAO模式中很常見。

+0

我添加了你放下的方法,在第一個帳戶被讀入鏈接列表後,正在添加另一行。我該如何做到這一點,以便當它找到一個空行時,它只是跳過它,並讀取下一行? – 2012-03-07 02:06:01

+0

好吧,所以基本上我用你的方法。而且,由於Stephen C的工作,所以我將額外的行添加到了我的readfile方法中。 – 2012-03-07 02:40:40

+0

是的,我錯了,它仍然沒有工作 – 2012-03-07 03:34:53

0

爲我工作。問題似乎是您的file.txt有一個額外的換行Walkinglily之間。仔細檢查文件並確保沒有文件。

+0

我的文本文件中沒有多餘的行或空格......它只是不能正確顯示。 – 2012-03-07 01:22:24

0

問題顯然不在輸出端。 toString()中的錯誤不會導致它產生像這樣的輸出。

它看起來像是每個記錄後輸入一個額外的空行。這會導致您的read方法在循環的每次迭代中增加一行。如果這是問題,則修復方法是添加一個額外的readLine調用以跳過空白行。


在另一張紙條上,您對班級名稱的選擇特別差。 LinkedAccountList類不代表任何東西的列表。它代表一個帳戶。

+0

好吧生病了試試吧 – 2012-03-07 01:27:22

+0

我要更改類名 – 2012-03-07 01:30:11

+0

在while循環的末尾添加一行額外幫助,但仍然存在問題。一旦到達最後兩個帳戶,它將跳過第二個帳戶的用戶名,並跳過最後一個帳戶的密碼。 – 2012-03-07 01:36:16