如何从文本文件中读取数据并将其添加到链表中
How to read data from a textfile and add it to a linked list
我正在尝试制作一个程序来保存医院的患者名单。
我正在尝试从文本文件中读取数据并将其添加到链表的第一个节点。
我对如何从文本文件中读取数据并将其添加到列表感到困惑。
这是我目前尝试过的
我的病人 Class:
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;
}
}
我的链表class:
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 对象添加到链表中。如有任何帮助,我们将不胜感激。
在您的 Patient class 中添加一个构造函数,它将在您的主要方法中使用:
public Patient()
{
}
在您的 PatientApplicationTester class 的主要方法中将其替换为以下代码:
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
最后在你的 Patients Class 中 setter 方法是错误的你需要给它添加参数例如
public void set_name(String name)
{
this.name = name;
}
我正在尝试制作一个程序来保存医院的患者名单。 我正在尝试从文本文件中读取数据并将其添加到链表的第一个节点。
我对如何从文本文件中读取数据并将其添加到列表感到困惑。
这是我目前尝试过的 我的病人 Class:
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;
}
}
我的链表class:
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 对象添加到链表中。如有任何帮助,我们将不胜感激。
在您的 Patient class 中添加一个构造函数,它将在您的主要方法中使用:
public Patient()
{
}
在您的 PatientApplicationTester class 的主要方法中将其替换为以下代码:
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
最后在你的 Patients Class 中 setter 方法是错误的你需要给它添加参数例如
public void set_name(String name)
{
this.name = name;
}