【问题标题】:Inserting text file input into a linked list将文本文件输入插入链接列表
【发布时间】:2013-11-14 16:48:31
【问题描述】:

大家好,我正在尝试从文本文件中读取并将每个名称存储到链表节点中。当我读入文本文件时,它会读取该行,这是一个名称。我正在尝试将每个名称存储到一个链表节点中。当我调用 insertBack 方法并打印出来时,它显示节点中没有任何内容。谁能指出我正确的方向,不胜感激?

这是类中的文件:

import java.util.Scanner;

import java.io.*;

public class fileIn {
    String fname;

    public fileIn() {
        getFileName();
        readFileContents();
    }

    public void readFileContents()
    {
        boolean looping;
        DataInputStream in;
        String line;
        int j, len;
        char ch;

        /* Read input from file and process. */
        try {
            in = new DataInputStream(new FileInputStream(fname));
            LinkedList l = new LinkedList();

            looping = true;
            while(looping) {
                /* Get a line of input from the file. */
                if (null == (line = in.readLine())) {
                    looping = false;
                    /* Close and free up system resource. */
                    in.close();
                }
                else {
                    System.out.println("line = "+line);
                    j = 0;
                    len = line.length();
                    for(j=0;j<len;j++){
                        System.out.println("line["+j+"] = "+line.charAt(j));
                    }
                }
                l.insertBack(line);
            } /* End while. */

        } /* End try. */

        catch(IOException e) {
            System.out.println("Error " + e);
        } /* End catch. */
    }

    public void getFileName()
    {
        Scanner in = new Scanner(System.in);

        System.out.println("Enter file name please.");
        fname = in.nextLine();
        System.out.println("You entered "+fname);
    }

}

这是 LinkedListNode 类:

public class LinkedListNode
{

    private String data;
    private LinkedListNode next;


    public LinkedListNode(String data)
    {
        this.data = data;
        this.next = null;
    }

    public String getData()
    {
        return data;
    }

    public LinkedListNode getNext()
    {
        return next;
    }

    public void setNext(LinkedListNode n)
    {
        next = n;
    }
}

最后是具有 main 方法的 LinkedList 类:

import java.util.Scanner;

public class LinkedList {

    public LinkedListNode head;

    public static void main(String[] args) {

        fileIn f = new fileIn();
        LinkedList l = new LinkedList();
        System.out.println(l.showList());   
    }

    public LinkedList() {
        this.head = null;
    }

    public void insertBack(String data){
        if(head == null){
            head = new LinkedListNode(data);
        }else{
            LinkedListNode newNode = new LinkedListNode(data);
            LinkedListNode current = head;
            while(current.getNext() != null){
                current = current.getNext();
            }
            current.setNext(newNode);
        }       
    }

    public String showList(){
        int i = 0;
        String retStr = "List nodes:\n";
        LinkedListNode current = head;
        while(current != null){
            i++;
            retStr += "Node " + i + ": " + current.getData() + "\n";
            current = current.getNext();

        }

        return retStr;
    }
}

【问题讨论】:

  • 您是否在控制台中看到从调试语句中打印出的读取文本?
  • 我会推荐BufferedReaderInputStreamReader 而不是DataInputStreamDataInputStream.readLine 已弃用。
  • 在控制台中,我可以看到它在哪里显示 line=Shawn(或任何其他名称)广告,然后显示每个字母都存储在 line[j] 上的数组中。当它到达我的 showList 方法时,它只显示 List Nodes: 之后没有其他内容。
  • 啊,很高兴知道,我会在试图找出问题所在的同时尝试改变它。

标签: java file-io linked-list


【解决方案1】:

问题是您在fileIn 中创建了LinkedList

但是你不导出它:

  fileIn f = new fileIn();
  LinkedList l = new LinkedList();

你需要的是这样的:

  fileIn f = new fileIn();
  LinkedList l = f.readFileContents(String filename, new LinkedList()); 

更改方法以使用您创建的LinkedList,然后填充它。所以fileIn 类可能看起来像这样:

public class fileIn {
    ...
    public void readFileContents(String fileName, LinkedList) {
        // fill linked list
    }
    ...
}

【讨论】:

  • @PaulDraper in fileIn::readFileContents() 他创建并填充了一个 LinkedList,然后从不导出它。
  • 您是否建议我将其重写为 LinkedList 类的导出行并在那里创建列表?还是一样有效?
  • @Shawn 我建议你创建一个方法来获取并返回LinkedList,例如public void readFileContents(LinkedList list)。然后称它为fileIn.readFileContents(new LinkedList()) 或类似的更灵活且更清楚你在做什么的东西。此外,就像另一条评论所述,使用BufferedReader 而不是DataInputStream。并摆脱那个丑陋的 for(,,,) 循环。只需做一个while((line = br.readLine())!=null) ...填充linkedList。搜索或阅读一些 Java 教程,你就会明白了。
  • 您可以通过多种方式做到这一点。只要确保您填写的LinkedList 是可访问的。帮自己一个忙,访问“Java Ranch”或在线 Java 教程。他们将帮助您编写清晰有效的代码。
猜你喜欢
  • 2015-02-20
  • 2017-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-14
  • 1970-01-01
  • 2012-12-29
  • 2023-03-20
相关资源
最近更新 更多