有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java将txt文件读入ObjectList

我正在尝试将文本文件读入对象的线性链接列表。文本文件(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进行一些添加和删除修改,我只是不确定这是将文件读入列表的正确编码

我得到一个nullpointerexception@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();
    }

}

共 (3) 个答案

  1. # 1 楼答案

    在调用ObjectList变量的方法之前,需要初始化该变量。在java中,默认情况下对象为空

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

    你还需要一个去污器,它是一个或多个空白

    lineScanner.useDelimiter("\\s+");
    
  2. # 2 楼答案

    自Java 7以来:

    List<String> lst = Files.readAllLines(Paths.get("payfile.txt"));
    
  3. # 3 楼答案

    初始化对象列表

    ObjectList first = new ObjectList();
    

    您试图对未初始化的对象调用一个方法,因此NPE as object的默认值为null

    你还必须初始化所有的ObjectList。在PayRoll的构造函数中进行初始化

    public Payroll(LineWriter lw)
      {
          this.lw = lw;
          this.firstName = new ObjectList();
          // do the same thing for your lastname, gender .. etc
      }