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

时间:2015-02-28 02:21:38

标签: java text constructor

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

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

   }
}

2 个答案:

答案 0 :(得分:0)

您的问题是您创建的列表是consructor的本地变量:

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

因此,构造函数收集的任何数据都不会存储在实例中。 你应该让那个lst成为班上的一员。

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可以使用该列表,而无需再次阅读该文件(就像目前一样)。

答案 1 :(得分:0)

你的

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

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

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