gpt4 book ai didi

java - 将 txt 文件读入 ObjectList

转载 作者:行者123 更新时间:2023-11-29 09:09:19 25 4
gpt4 key购买 nike

我正在尝试将文本文件读入线性对象链表。文本文件 (payfile.txt) 包含以下信息

DEBBIE     STARR           F 3 W 1000.00
JOAN JACOBUS F 9 W 925.00
DAVID RENN M 3 H 4.75
ALBERT CAHANA M 3 H 18.75
DOUGLAS SHEER M 5 W 250.00
SHARI BUCHMAN F 9 W 325.00
SARA JONES F 1 H 7.50
RICKY MOFSEN M 6 H 12.50
JEAN BRENNAN F 6 H 5.40
JAMIE MICHAELS F 8 W 150.00

我想将名字插入到他们自己的列表中,将姓氏插入到列表中,依此类推。我必须在整个问题的其余部分对 LLL 进行一些添加和删除修改,我只是不确定这是将文件读入列表的正确编码。

我得到一个空指针异常@ firstname.addFirst(lineScanner.next());

到目前为止我有这个:

 public class Payroll
{
private LineWriter lw;
private ObjectList output, input;
private ObjectList firstname, lastname, gender, tenure, rate, salary;

public Payroll(LineWriter lw)
{
this.lw = lw;
}
public void readfile()
{
File file = new File("payfile.txt");
try{
Scanner scanner = new Scanner(file);
while(scanner.hasNextLine())
{
String line = scanner.nextLine();
Scanner lineScanner = new Scanner(line);
lineScanner.useDelimiter(" ");
while(lineScanner.hasNext())
{
firstname.addFirst(lineScanner.next());
lastname.addFirst(lineScanner.next());
gender.addFirst(lineScanner.next());
tenure.addFirst(lineScanner.next());
rate.addFirst(lineScanner.next());
salary.addFirst(lineScanner.next());
}
}
}catch(FileNotFoundException e)
{e.printStackTrace();}
}



// ObjectList.java
public class ObjectList {
private ListNode list;

/**
* Constructs an empty list
*/
public ObjectList() {
list = null;
}

/**
* Returns the first node in the list
*/
public ListNode getFirstNode() {
return list;
}

/**
* Returns the first element in the list
*/
public Object getFirst() {
if (list == null)
return null;
return list.getInfo();
}

/**
* Returns the last element in the list
*/
public Object getLast() {
if (list == null)
return null;
ListNode p = list;
while (p.getNext() != null)
p = p.getNext();
return p.getInfo();
}

/**
* Adds the given element to the beginning of the list
* @param o - the element to be inserted at the beginning of the list
*/
public void addFirst(Object o) {
ListNode p = new ListNode(o, list);
list = p;
}

/** Appends the given element to the end of the list
* @param o - the element to be appended to the end of the list
*/
public void addLast(Object o) {
ListNode p = new ListNode(o);
if (list == null)
list = p;
else {
ListNode q = list;
while (q.getNext() != null)
q = q.getNext();
q.setNext(p);
}
}

/**
* Removes and returns the first element from the list
*/
public Object removeFirst() {
if (list == null) {
System.out.println("removeFirst Runtime Error: Illegal Operation");
System.exit(1);
}
ListNode p = list;
list = p.getNext();
return p.getInfo();
}

/**
* Removes and returns the last element from the list
*/
public Object removeLast() {
if (list == null) {
System.out.println("removeLast Runtime Error: Illegal Operation");
System.exit(1);
}
ListNode p = list;
ListNode q = null;
while (p.getNext() != null) {
q = p;
p = p.getNext();
}
if (q == null)
list = null;
else
q.setNext(null);
return p.getInfo();
}

/**
* Inserts a node after the node referenced by p
* @param p - reference to node after which the new node will be added
* @param o - reference to node that will be inserted into the list
*/
public void insertAfter(ListNode p, Object o) {
if (p == null) {
System.out.println("insertAfter Runtime Error: Illegal Operation");
System.exit(1);
}
ListNode q = new ListNode(o, p.getNext());
p.setNext(q);
}

/**
* Deletes the node after the node referenced by p
* @param p - reference to node after which the node will be deleted
*/
public Object deleteAfter(ListNode p) {
if (p == null || p.getNext() == null) {
System.out.println("deleteAfter Runtime Error: Illegal Operation");
System.exit(1);
}
ListNode q = p.getNext();
p.setNext(q.getNext());
return q.getInfo();
}

/**
* Inserts a node into its correct location within an ordered list
* @param o - The element to be inserted into the list
*/
public void insert(Object o) {
ListNode p = list;
ListNode q = null;
while (p != null && ((Comparable)o).compareTo(p.getInfo()) > 0) {
q = p;
p = p.getNext();
}
if (q == null)
addFirst(o);
else
insertAfter(q, o);
}

/**
* Removes and returns the first occurrence of the specified
* element in the list
* @param o - The object to be removed from the list
*/
public Object remove(Object o) {
ListNode p = list;
ListNode q = null;
while (p != null && ((Comparable)o).compareTo(p.getInfo()) != 0) {
q = p;
p = p.getNext();
}
if (p == null)
return null;
else return q == null ? removeFirst() : deleteAfter(q);
}

/**
* Returns true if the list contains the specified element.
* @param o - The object to search for in the list
*/
public boolean contains(Object o) {
ListNode p = list;
while (p != null && ((Comparable)o).compareTo(p.getInfo()) != 0)
p = p.getNext();
return p != null;
}

/**
* Returns true if this list contains no elements
*/
public boolean isEmpty() {
return list == null;
}

/**
* Removes all elements from the list
*/
public void clear() {
list = null;
}

/**
* Returns the number of elements in the list
*/
public int size() {
int count = 0;
ListNode p = list;
while (p != null) {
++count;
p = p.getNext();
}
return count;
}
}

public class Driver
{

public static void main(String args[])
{
LineWriter lw = new LineWriter("csis.txt");
Payroll payroll = new Payroll(lw);

payroll.readfile();
// payroll.printer(lw);
lw.close();
}

}

最佳答案

在调用方法之前,您需要初始化 ObjectList 变量。 Java 中默认 Object 为 null

firstname = new ObjectList();
firstname.addFirst(lineScanner.next());

你还需要一个或多个空格的分隔符

lineScanner.useDelimiter("\\s+");

关于java - 将 txt 文件读入 ObjectList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13212443/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com