将文本文件中的名称添加到链接列表中 [英] Adding names from a text file into a linked list

查看:67
本文介绍了将文本文件中的名称添加到链接列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,所以我遇到了关于如何将文本文件中的名称添加到链接列表中的问题。该代码非常适合从文本文件中读取名称(甚至可以显示我将在以后使用的每个字符索引)。所以这些名字都被读入并且效果很好,但是我有一个问题,想知道如何将它们添加到我的链表中。我有一个insertBack方法,但我不知道如何调用它或它是否正确设置。任何关于做什么或正确方向的建议都会非常感激!



这是我的LinkedListNode类:

  public   class  LinkedListNode 
{

private String 数据;
private LinkedListNode next;


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

public String getData()
{
返回数据;
}

public LinkedListNode getNext()
{
返回下一步;
}

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





这是我用我所有方法的LinkedList类:

  import  java.io. *; 
import java.util.Scanner;

public class LinkedList {

public LinkedListNode head;
String fname;

public static void main( String [] args) throws FileNotFoundException {
LinkedList l = new LinkedList();
l.insertBack();
System.out.print(l.showList());
}


public LinkedList(){
.head = null;
getFileName();
readFileContents();

}

public void readFileContents()
{
boolean 循环;
DataInputStream in;
String 行;
int j,len;
char ch;

/ * 从文件和流程中读取输入。 * /
尝试 {
in = new DataInputStream(< span class =code-keyword> new FileInputStream(fname));

looping = true;
while (循环){
/ * 从文件中获取一行输入。 * /
if (null ==(line = in.readLine())){
looping = false;
/ * 关闭并释放系统资源。 * /
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));
}
}
} / * 结束时间。 * /

} / * 结束尝试。 * /

catch (IOException e){
System.out.println( 错误 + e);
} / * 结束捕获。 * /
}

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

System.out.println( 请输入文件名。) ;
fname = in.nextLine();
System.out.println( 您输入了 + fname);
}


public void insertBack(< span class =code-sdkkeyword> 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 ,j;
字符串 retStr = 列表节点:\\ \
;
LinkedListNode current = head;
while (current!= null){
i ++;
retStr + = 节点 + i + + current.getData()+ \\\
;
current = current.getNext();

}

return retStr;
}
}





现在我的错误是在我的主要调用insertBack方法(显然不是以它的方式工作,因为它没有参与任何争论)。我试图弄清楚如何从readFileContents中获取行并将其插入到insertBack方法中。如果我让任何人感到困惑,请随意提出任何问题。

解决方案

让我们从第一件物品开始,从第一眼就看出来,然后继续你自己的。



首先,在类 LinkedList中放入 readFileContents 是一个很大的设计错误。删除此方法,它与链表功能无关。读取其他类中的文件(例如,创建一个类 TestLinkedList 并将引用或链接列表传递给它或创建 LinkedList的实例在这个课程中)。并且,或者当然,删除 getFileName ,与输入输出相关的任何内容, Main 等等。链接的类list应该是自给自足的,它不应该包含使用列表的任何代码。



其次,你在你的循环中遍历文件的行,你不甚至尝试使用链表。你明白没有奇迹吗? :-)。在你循环中,使用 insert (你有这样的方法吗?在哪里?), inserBack 或其他更新列表的东西内容。



希望这足以继续。



-SA

Hey guys, so I am having this issue on how to add names from a text file into a linked list. The code works great to read the names from the text file in (and even shows me each character index that I will use later down the road). So the names are read in and that works great, but I am having an issue figuring out how to add them to my linked list. I have an insertBack method, but am not sure how to call it or if it is even set up properly. Any advice on what to do or a point in the right direction would be greatly appreciated!

This is my LinkedListNode class:

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;
    }
}



This is the LinkedList class with all my methods:

import java.io.*;
import java.util.Scanner;

public class LinkedList {

    public LinkedListNode head;
    String fname;

    public static void main(String[] args) throws FileNotFoundException{
            LinkedList l = new LinkedList();
            l.insertBack();
            System.out.print(l.showList());
        }


    public LinkedList() {
        this.head = null;
        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));
            
            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));
                	}
                }
            } /* 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);
    }


    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, j;
        String retStr = "List nodes:\n";
        LinkedListNode current = head;
        while(current != null){
            i++;
            retStr += "Node " + i + ": " + current.getData() + "\n";
            current = current.getNext();

        }

        return retStr;
    }
}



Now my error is calling the insertBack method in my main(which obviously doesn't work the way it is because it is not taking in any argument). I am trying to figure out how to get the lines from the readFileContents and insert those into the insertBack method. Feel free to ask anything if I am confusing anybody.

解决方案

Let's start with first too items, which are apparent from the first glance, and then please continue of your own.

First, it's a big design mistake to put readFileContents in the class LinkedList. Remove this method, it has nothing to do with linked list functionality. Read file in some other class (say, create a class TestLinkedList and pass a reference or the linked list to it or create an instance of LinkedList in this class). And, or course, remove getFileName, anything related to input-output, Main, etc. The class of your linked list should be self-sufficient and it should not contain any code using the list.

Secondly, you in your loop traversing the lines of the file, you don't even try to work with a linked list. Do you understand that there is no such thing as miracle? :-). Inside you loop, use insert (do you have such method? where?), inserBack or something else updating the list content.

Hope this is enough to continue.

—SA


这篇关于将文本文件中的名称添加到链接列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆