0

テキストファイルから読み込んで、各名前をリンクリストノードに保存しようとしています。テキストファイルを読み込むと、名前である行が読み込まれます。それぞれの名前をリンク リスト ノードに格納しようとしています。insertBack メソッドを呼び出して出力すると、ノードに何もないことがわかります。誰かが私を正しい方向に向けることができますか?

fileIn クラスは次のとおりです。

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

1 に答える 1

2

The problem is that you create the LinkedList in your fileIn.

But then you do not export it:

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

What you need is something like this:

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

Change the method to use the LinkedList you created and then populate it. So the fileIn class might look like something like this:

public class fileIn {
    ...
    public void readFileContents(String fileName, LinkedList) {
        // fill linked list
    }
    ...
}
于 2013-11-04T04:06:37.633 に答える