为什么我的构造函数不能正常工作?

Why is my Constructor not working properly?

我想要的是让我的构造函数过滤掉我在文本文件中指定的内容,然后根据过滤后的文本文件使用我的 getLongestWord 方法。 我试图让包含 0-9 的单词被忽略,并且单词中的任何标点符号在存储之前都会被删除。纯标点符号的单词将被忽略。在构造函数returns之后,新实例将拥有它进行分析所需的所有信息;将不再需要该文件。

public class TextProcessorImpl implements TextProcessor {

private String filename;

public TextProcessorImpl(String filename) {
    this.filename = filename;
    String current;
    Scanner scan = TextReader.openFile(filename);
    ArrayList<String> lst = new ArrayList<String>();
    while (scan.hasNext()) {
        current = scan.next();
        if (current.matches(".*[0-9].*")) {

        }
        else {
            current = current.replaceAll("\p{Punct}+", "");
            if (current.isEmpty()) {
            }
            else {
                lst.add(current);
            }
        }
    }
}

@Override
public Collection<String> getLongestWords() {


    String longestWord = "";
    String current;
    Scanner scan = TextReader.openFile(filename);   // Generate scanner
    ArrayList<String> lst = new ArrayList<String>();    //create array list
    while (scan.hasNext()) {    //while the text has a next word in it
        current = scan.next();  //set current to that next word
        if (current.length() > longestWord.length()) {  //if the current word length is greater than the longest word length
            longestWord = current;  //set the new longest word to current
            lst.clear();    //clear the previous array
            lst.add(longestWord);   //add the new longest word

        }
        else if( current.length() == longestWord.length()) { //else if the current word length = the longest word length
            if (!lst.contains(current)) {
                lst.add(current);   //add the current word to the array
            }
        }



    }return lst;

}

主程序:

public class TextAnalysis {

/**
 * Get a file name from the command line and ask a TextProcessor
 * to analyze it.
 *
 * @param args a single-element array containing the file name
 */
public static void main( String[] args ) {
    if ( args.length != 1 ) {
        System.err.println( "Usage: java TextProcessor file" );
        System.exit( 2 );
    }
    TextProcessor textProc = new TextProcessorImpl( args[ 0 ] );

    Collection< String > longestWords = textProc.getLongestWords();
    System.out.println( "Longest words: " + longestWords );

   }
}

您的问题是您创建的列表是构造函数的局部变量:

ArrayList<String> lst = new ArrayList<String>();

因此,构造函数收集的任何数据都不会存储在实例中。 您应该使 lst 成为 class.

的成员
public class TextProcessorImpl implements TextProcessor {

private String filename;
private ArrayList<String> lst = new ArrayList<String>(); 

public TextProcessorImpl(String filename) {
    this.filename = filename;
    String current;
    Scanner scan = TextReader.openFile(filename);
    ...

然后您的 getLongestWords 可以使用该列表而无需再次读取文件(就像现在一样)。

你的

ArrayList<String> lst = new ArrayList<String>();

在构造函数中声明并初始化。它是局部变量,不是实例变量。因此,即使您的逻辑工作正常,您也无法访问它。 您可以将声明部分移到构造函数之外。

ArrayList<String> lst;
public TextProcessorImpl(String filename) {
     lst = new ArrayList<String>();
     ....
}