如何从文本文件中读取数据并将其添加到链接列表中

时间:2015-04-03 16:56:45

标签: java io linked-list

我正在尝试制作一个包含医院病人名单的计划。 我正在尝试从文本文件中读取数据并将其添加到链接列表的第一个节点。

我对如何从文本文件中读取数据并将其添加到列表感到困惑。

这是我到目前为止所尝试的 我的患者班:

public class Patient implements Serializable {

private String name;
private String patientIDNumber;
private String address;
private int height;
private double weight;


protected LLNode<Patient> head;


public Patient(String n, String ID, String Ad, int h, double w)
{
    name = n;
    patientIDNumber = ID;
    address = Ad;
    height = h;
    weight = w;


}

public String get_name()
{
    return name;
}

public String get_patientIDNumber()
{
    return patientIDNumber;
}

public String get_address()
{
    return address;
}

public int get_height()
{
    return height;
}

public double get_weight()
{
    return weight;
}

public void set_name()
{
    this.name = name;
}

public void set_patientIDNumber()
{
    this.patientIDNumber = patientIDNumber;
}

public void set_address()
{
    this.address = address;
}

public void set_height()
{
    this.height = height;
}

public void set_weight()
{
    this.weight = weight;
}

}

我的链表类:

public class PatientList<T> implements ListInterface<T>{

protected int numOfElements;
protected LLNode<T> currentPos;
protected boolean found;
protected LLNode<T> location;
protected LLNode<T> previous;

protected LLNode<T> list;
protected LLNode<T> tail;

public PatientList()
{
    numOfElements = 0;
    list = null;
    currentPos = null;
}

public void add(T element)
{
    LLNode<T> newNode = new LLNode<T> (element);
    newNode.setLink(list);
    list = newNode;
    numOfElements++;
}

protected void find(T target)
{
    location = list;
    found = false;

    while(location != null)
    {
        if(location.getInfo().equals(target))
        {
            found = true;
            return;
        }
        else
        {
            previous = location;
            location = location.getLink();
        }
    }
}

public int size()
{
    return numOfElements;
}

public boolean contains(T element)
{
    find(element);
    return found;
}

public boolean remove(T element)
{
    find(element);
    if(found)
    {
        if(list == location)
            list = list.getLink();
        else
            previous.setLink(location.getLink());

        numOfElements++;
    }
    return found;
}
public T get(T element)
{
    find(element);
    if(found)
        return location.getInfo();
    else
        return null;
}

public String toString()
{
    LLNode currNode = list;
    String listString = "List:\n";
    while(currNode != null)
    {
        listString = listString + "  " + currNode.getInfo() + "\n";
        currNode = currNode.getLink();
    }
    return listString;
}
public void reset()
{
    currentPos = list;
}

public T getNext()
{
    T next = currentPos.getInfo();
    if (currentPos.getLink() == null)
        currentPos = list;
    else
        currentPos = currentPos.getLink();
    return next;
}

}

我的文本文件如下所示:

Random Name
1024 
Random Ln NY
70 
185
Joe Smith
1025
134 Nowhere Lane New York NY
80 
170

我一直试图读取这样的数据:

import java.io.*;
import java.util.*;

public class PatientApplicationTester {
    public static void main(String[] args) throws ClassNotFoundException
    {
        PatientList<Patient> list = new PatientList<Patient>();
        try {
            File f = new File("Patients.txt");
            FileReader r = new FileReader(f);
            BufferedReader reader = new BufferedReader(r);

            String line = null;

            while((line = reader.readLine()) != null)  {
                System.out.println(line);

        }
        reader.close();
    } catch(IOException e) {
        e.printStackTrace();
    }

}

}

我知道这只是逐行读取然后打印出来,我真的很困惑我将如何阅读数据,然后作为Patient对象添加到链接列表。任何帮助将不胜感激,谢谢。

1 个答案:

答案 0 :(得分:0)

Patient 类中添加一个将在main方法中使用的构造函数:

public Patient()
{

}

在main方法的 PatientApplicationTester 类中,将其替换为以下代码:

public static void main(String[] args) throws ClassNotFoundException
    {
        PatientList<Patient> list = new PatientList<Patient>();
        try {
            File f = new File("Patients.txt");
            FileReader r = new FileReader(f);
            BufferedReader reader = new BufferedReader(r);

            String line = null;
            Patient patient = new Patient(); // create Object use the constructor that I added.
            int i = 1; // this variable is used to know on which line the reader is on
            while((line = reader.readLine()) != null)  
            {
               if(line.trim().equals("")) // if the line is a new line then add the previous patient to the list and create a new patient, re initialize i = 0
               {
                    list.add(patient);
                    patient = new Patient();
                    i = 1;
               }
               else
               {
                    if(i == 1) // on first line of the file set the patients name
                    {
                        patient.set_name(line);
                    }
                    else if(i == 2)//on second line of the file set the patients ID Number
                    {
                        patient.set_patientIDNumber(line);
                    }
                    // add else if for other attributes on other lines
                    i++;
               }    
            }
             list.add(patient);

             reader.close();

        }catch(IOException e) {
            e.printStackTrace();
        }
    }

使用如下所示的新行在 Patients.txt 文件中分隔您的患者:

Random Name
1024 
Random Ln NY
70 
185

Joe Smith
1025
134 Nowhere Lane New York NY
80 
170

最后在患者类中,setter方法是错误的,您需要为其添加参数,例如

public void set_name(String name)
{
    this.name = name;
}