将文本文件加载到单链列表中

时间:2019-02-20 20:16:07

标签: java nodes

因此,我目前正在为计算机科学入门课的一项作业做作业,而我只是为作业的基本要素而苦苦挣扎。我想将单词的文本文件加载到单链列表结构中,但似乎无法使它正常工作。

我的教授已经给了一个可以从文件“ WordReader”中读取单词的类,我们必须使用“ readWord()”函数从文本文件中读取每个单词。但是,我似乎无法在我的主程序中实现它。

代码如下。

**** WordReader类:****

package Assign_2;


import BasicIO.*;
import static java.lang.Character.*;


public class WordReader {


    private ASCIIDataFile  source;  // source of text for reading words


    /** The constructor initializes the reader opening an ASCIIDataFile to read
      * from.                                                                    */

    public WordReader ( ) {

        source = new ASCIIDataFile();;

    };  // construtor


    /** This method returns true if the underlying data file has reached EOF.
      * 
      * @return  boolean  true if the file has reached EOF.                      */

    public boolean isEOF ( ) {

        return source.isEOF();

    };  // isEOF


    /** This method returns the next word (string of alphabetic characters) in the
      * data file. If the end of file is reached, a subsequent call to isEOF will
      * return true and the result of readWord is undefined.                     */

    public String readWord ( ) {

        String  result;  // next word read
        char    c;       // next character in input

        result = "";
        for ( ; ; ) {  // skip to alphabetic
            c = source.readC();
        if ( source.isEOF() || isLetter(c) ) break;
        };
        while ( !source.isEOF() && isLetter(c) ) {
            result = result + c;
            c = source.readC();
        };
        return result;

    };  // readWord


    /** This method closes the WordReader                                          */

    public void close ( ) {

        source.close();

    };  // close


}  // WordRerader

节点类

package Assign_2;

class Node {


    String   item;   // the words
    Node     next;  // next node


    Node ( String c, Node n ) {

        item = c;
        next = n;

    };  // constructor

}  //Node

主要班级

package Assign_2;

import BasicIO.*;


public class Assign_2 { 

  private Node            list;     // list of all unique words
  private ASCIIDisplayer  display;  //display of list


  public Assign_2 () {

    display = new ASCIIDisplayer();
    list = null;


  }; // Constructor

  private void loadWords () {

    ASCIIDataFile     story;   //file of Alice's story
    String        text;
    String            word;

    story = new ASCIIDataFile();
    for ( ; ; ) {
      text = new String (story);
    if ( story.isEOF() ) break;
      word = WordReader.readWord();
    };


  }; //loadWords

  private void Buildlist( String word ) {

    Node p;
    Node q;

    p = list;
    q = null;

    while ( p != null ) {
      q = p;
      p = p.next;
    };
    if (q == null) {
      list = new Node (word, null);       //only for first word
    }
    else {
      q.next = new Node (word, null);     //for adding remaining words
    };


  } // Buildlist









    public static void main ( String[] args ) { Assign_2 c = new Assign_2(); }; 



} 

0 个答案:

没有答案