0
我正在嘗試製作一個包含醫院患者列表的程序。 我想從文本文件讀取數據並將其添加到鏈接列表的第一個節點。如何從文本文件中讀取數據並將其添加到鏈接列表
我很困惑我該如何讀取文本文件中的數據並將其添加到列表中。
這是我已經試過到目前爲止 我的病人類別:
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對象添加到鏈接列表中。任何幫助將不勝感激謝謝。
請剛剛閱讀了關於如何解析了Java中的字符串(最這些值可以直接作爲字符串放入,只需將高度和重量解析爲整數)。一旦你弄清楚了,你應該能夠創建一個帶有解析值的'Patient'對象並將它添加到你的列表中。 – tnw 2015-04-03 17:00:04