Java 为什么赢了';我的程序是否读取整个文件并将数据保存在链表中?

Java 为什么赢了';我的程序是否读取整个文件并将数据保存在链表中?,java,file-io,linked-list,Java,File Io,Linked List,我的程序没有完全导入文件时出现问题。它似乎只导入文件的一行,并表示已读取该文件,而未将数据保存在链接列表中的节点中。当我试图显示来自第一个节点的数据时,程序崩溃。以下是我的importFile类代码: //Class to read the file class InputFile { //Global Variables static String fileLocation = "E:\\ScottN_MergeSort&InsertSortUpdate3"

我的程序没有完全导入文件时出现问题。它似乎只导入文件的一行,并表示已读取该文件,而未将数据保存在链接列表中的节点中。当我试图显示来自第一个节点的数据时,程序崩溃。以下是我的importFile类代码:

//Class to read the file
class InputFile
{

    //Global Variables
    static String fileLocation = "E:\\ScottN_MergeSort&InsertSortUpdate3"
            + "\\ScottN_MergeSort&InsertSort\\src\\scottn_mergesort\\insertsort"
            + "\\us1000000keyEditedEdited.csv";

    //FileReader
    static FileReader fileRead = null;

    //BufferedReader
    static BufferedReader br;

    public static void readInputFile()
    {
        //Variables
        Node current = new Node();

        //Note need to read data into ten variables
        try
        {
            fileRead = new FileReader(fileLocation);
            br = new BufferedReader(new FileReader(fileLocation));
            String lineline = br.readLine();

//            String line = br.readLine();
            while (current != null)
            {
                lineline = br.readLine();

                //This will split the line into sections based on the 
                //placement of comas
                String[] readLine = lineline.split(",");

                //Insert data from the line's array into the linked list
                LinkedList.head = current;

                current.setFirstName(readLine[0]);
                current.setLastName(readLine[1]);
                current.setCompany(readLine[2]);
                current.setAddress(readLine[3]);
                current.setCity(readLine[4]);
                current.setCounty(readLine[5]);
                current.setState(readLine[6]);
                current.setZip(Integer.parseInt(readLine[7]));
                current.setPhone((readLine[8]));
                current.setKey(Double.parseDouble(readLine[9]));

                //Assign initial row number
                LinkedList.AssignRowNum();

                System.out.println("....Still reading the file....");
                //    System.out.println(line);
//            line = br.readLine();

                current = current.next;

            }//End of while loop
            System.out.println("The file has been read. ");

        } catch (IOException e)
        {
            System.out.println("Unable to read file. ");
        }

    }
你知道我做错了什么吗

这里是我的各个节点和链表的类。我忘记上传了

//Base class for building linked list
class Node
{

    //Global Variables
    private String firstName;
    private String lastName;
    private String company;
    private String address;
    private String city;
    private String county;
    private String state;

    private int zip;
    private String phone;
    private double key;
    private int rowNum;


    Node next;

    //Constructors
    public Node()
    {

    }

    public Node(String firstName, String lastName, String company,
            String address, String city, String county,
            String state, int zip, String phone, int key)
    {
        this.firstName = firstName;
        this.lastName = lastName;
        this.company = company;
        this.address = address;
        this.city = city;
        this.county = county;
        this.state = state;
        this.zip = zip;
        this.phone = phone;
        this.key = key;
//        this.rowNum = rowNum;
    }

    public Node(Node next)
    {
        this.next = next;
    }

    public Node(int rowNum)
    {
        this.rowNum = rowNum;
    }

    //Getters and Setters
    public String getFirstName()
    {
        return firstName;
    }

    public void setFirstName(String firstName)
    {
        this.firstName = firstName;
    }

    public String getLastName()
    {
        return lastName;
    }

    public void setLastName(String lastName)
    {
        this.lastName = lastName;
    }

    public String getCompany()
    {
        return company;
    }

    public void setCompany(String company)
    {
        this.company = company;
    }

    public String getAddress()
    {
        return address;
    }

    public void setAddress(String address)
    {
        this.address = address;
    }

    public String getCity()
    {
        return city;
    }

    public void setCity(String city)
    {
        this.city = city;
    }

    public String getCounty()
    {
        return county;
    }

    public void setCounty(String county)
    {
        this.county = county;
    }

    public String getState()
    {
        return state;
    }

    public void setState(String state)
    {
        this.state = state;
    }

    public int getZip()
    {
        return zip;
    }

    public void setZip(int zip)
    {
        this.zip = zip;
    }

    public String getPhone()
    {
        return phone;
    }

    public void setPhone(String phone)
    {
        this.phone = phone;
    }

    public double getKey()
    {
        return key;
    }

    public void setKey(double key)
    {
        this.key = key;
    }

    public int getRowNum()
    {
        return rowNum;
    }

    public void setRowNum(int rowNum)
    {
        this.rowNum = rowNum;
    }

}


class LinkedList
{

    //Global Variables
    public static Node head;
    public static Node sortedHead;
    static public int totalNodes = 0;

    //Constructor
    public LinkedList()
    {
    }

    public LinkedList(int totalNodes, Node head, Node sortedHead)
    {
        this.head = head;
        this.sortedHead = sortedHead;
        this.totalNodes = totalNodes;
    }

    public LinkedList(int totalNodes)
    {
        this.totalNodes = totalNodes;
    }

    //Getters and Setters
    public static Node getHead()
    {
        return head;
    }

    public static void setHead(Node head)
    {
        LinkedList.head = head;
    }

    public static Node getSortedHead()
    {
        return sortedHead;
    }

    public static void setSortedHead(Node sortedHead)
    {
        LinkedList.sortedHead = sortedHead;
    }

    public static int getTotalNodes()
    {
        return totalNodes;
    }

    public static void setTotalNodes(int totalNodes)
    {
        LinkedList.totalNodes = totalNodes;
    }

    //Functions
    void push(String firstName, String lastName, String company,
            String address, String city, String county, String state,
            int zip, String phone, int key)
    {
        Node babyNode = new Node(firstName, lastName, company, address,
                city, county, state, zip, phone, key);

        totalNodes++;
        babyNode.next = head;
        head = babyNode;

    }

    public static void printLinkedList()
    {
        Node current = head;

        while (current != null)
        {
            //Print at least the names with the key but you do not have to 
            //include all of the information.

            //Perhaps allow user to get the information upon request
            System.out.println(current.getFirstName() + " " + current.getLastName()
                    + " " + current.getCompany() + " " + current.getAddress() + " "
                    + current.getCity() + " " + current.getCounty() + " " + current.getState()
                    + " " + current.getZip() + " " + current.getPhone() + " "
                    + current.getKey() + " ");

            current = current.next;
        }
    }

    public static void printLinkedListByRowNum(int beginRowNum, int exitRowNum)
    {
        Node current = head;

        while (current != null)
        {
            if (current.getRowNum() == beginRowNum)
            {
                while (current.getRowNum() != exitRowNum)
                {
                    //Print at least the names with the key but you do not have to 
                    //include all of the information.

                    //Perhaps allow user to get the information upon request
                    System.out.println(current.getFirstName() + " " + current.getLastName()
                            + " " + current.getCompany() + " " + current.getAddress() + " "
                            + current.getCity() + " " + current.getCounty() + " " + current.getState()
                            + " " + current.getZip() + " " + current.getPhone() + " "
                            + current.getKey() + " ");

                    current = current.next;
                }
            }
        }
    }

    //Function to assign a row number to each sorted node so that they can
    //be printed by row number
    static void AssignRowNum()
    {
        //Variables

        Node current = sortedHead;

        while (current != null)
        {
            for (int cntr = 1; cntr < (totalNodes + 1); cntr++)
            {
                current.setRowNum(cntr);
                current = current.next;
            }
            head = sortedHead;
        }
    }

}
下面是更新后的LinkedList类:

class LinkedList
{

    //Global Variables
    public static Node head;
    public static Node sortedHead;
    public static Node current;
    public static Node first;
    public static Node last;

    static public int totalNodes = 0;

    //Constructor
    public LinkedList()
    {
    }

    public LinkedList(int totalNodes, Node head, Node sortedHead)
    {
        this.head = head;
        this.sortedHead = sortedHead;
        this.totalNodes = totalNodes;
    }

    public LinkedList(int totalNodes)
    {
        this.totalNodes = totalNodes;
    }

    //Getters and Setters
    public static Node getHead()
    {
        return head;
    }

    public static void setHead(Node head)
    {
        LinkedList.head = head;
    }

    public static Node getSortedHead()
    {
        return sortedHead;
    }

    public static void setSortedHead(Node sortedHead)
    {
        LinkedList.sortedHead = sortedHead;
    }

    public static int getTotalNodes()
    {
        return totalNodes;
    }

    public static void setTotalNodes(int totalNodes)
    {
        LinkedList.totalNodes = totalNodes;
    }

    //Functions
    //Function to check if Linked List is empty
    public static boolean isEmpty()
    {
        return totalNodes == 0;
    }

    //Returns size of doubly linked list
    public static int size()
    {
        if (isEmpty())
        {
            System.out.println("The list is empty. ");
        }
        return totalNodes;
    }

    //Function to add a new node to the linked list
    void push(String firstName, String lastName, String company,
            String address, String city, String county, String state,
            int zip, String phone, double key)
    {
         //Create a new node
        Node babyNode = new Node();

          if (isEmpty())
        {
            first = babyNode;
        } else
        {
            last.next = babyNode;
            babyNode.prev = last;
        }

        last = babyNode;
        current = last;
        totalNodes++;

//        Node babyNode = new Node(firstName, lastName, company, address,
//                city, county, state, zip, phone, key);

        totalNodes++;
        babyNode.next = head;
        head = babyNode;

    }
}

你现在的工作是什么?那个节点还存在吗?我没有看到你在任何地方设置next,只是看起来它正在将next设置为它自己的next,我猜是null。对不起,我相信我在这个节点类中设置了next。我刚刚在帖子中添加了节点类和链表。这有用吗?我在一个构造函数中指定了node类中的next。公共节点(Node next){this.next=next;}头部应指向第一个节点。不要老是换脑袋。我认为现在发生的情况是,您将头设置到一个新节点,并且之前创建的列表将被垃圾回收。尝试在调试模式下运行代码并跟踪head。在实际代码中,您仍然没有设置节点的下一个位置。您正在使用默认节点构造函数创建一个名为current的节点,然后调用
current.next
,该函数返回空值,因为从未设置下一个节点。另外,@Goion所说的也是正确的,您不能一直重置列表的标题。请在代码中修复此错误:
lineline=br.readLine()在处理之前,您将读取它两次。一个在循环前,一个在循环内。
class LinkedList
{

    //Global Variables
    public static Node head;
    public static Node sortedHead;
    public static Node current;
    public static Node first;
    public static Node last;

    static public int totalNodes = 0;

    //Constructor
    public LinkedList()
    {
    }

    public LinkedList(int totalNodes, Node head, Node sortedHead)
    {
        this.head = head;
        this.sortedHead = sortedHead;
        this.totalNodes = totalNodes;
    }

    public LinkedList(int totalNodes)
    {
        this.totalNodes = totalNodes;
    }

    //Getters and Setters
    public static Node getHead()
    {
        return head;
    }

    public static void setHead(Node head)
    {
        LinkedList.head = head;
    }

    public static Node getSortedHead()
    {
        return sortedHead;
    }

    public static void setSortedHead(Node sortedHead)
    {
        LinkedList.sortedHead = sortedHead;
    }

    public static int getTotalNodes()
    {
        return totalNodes;
    }

    public static void setTotalNodes(int totalNodes)
    {
        LinkedList.totalNodes = totalNodes;
    }

    //Functions
    //Function to check if Linked List is empty
    public static boolean isEmpty()
    {
        return totalNodes == 0;
    }

    //Returns size of doubly linked list
    public static int size()
    {
        if (isEmpty())
        {
            System.out.println("The list is empty. ");
        }
        return totalNodes;
    }

    //Function to add a new node to the linked list
    void push(String firstName, String lastName, String company,
            String address, String city, String county, String state,
            int zip, String phone, double key)
    {
         //Create a new node
        Node babyNode = new Node();

          if (isEmpty())
        {
            first = babyNode;
        } else
        {
            last.next = babyNode;
            babyNode.prev = last;
        }

        last = babyNode;
        current = last;
        totalNodes++;

//        Node babyNode = new Node(firstName, lastName, company, address,
//                city, county, state, zip, phone, key);

        totalNodes++;
        babyNode.next = head;
        head = babyNode;

    }
}