Загрузка текстового файла в односвязные списки - PullRequest
0 голосов
/ 20 февраля 2019

, поэтому я сейчас работаю над заданием для вводного курса по информатике, и я боролся только с основным элементом задания.Я хочу загрузить текстовый файл слов в односвязную структуру списка, но я не могу заставить его работать.

Мой профессор уже дал класс, который читает слова из файла «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(); }; 



} 
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...