Java:在我的类中为LinkedLists添加下划线红色的方法?

时间:2017-03-09 22:26:37

标签: java methods linked-list add cannot-find-symbol

我正在尝试创建一个程序,该程序采用常用单词列表,扫描书籍并计算每个单词的出现次数!一切都很顺利,但是:

public static void main(String[] args) {
    try {

        LinkedList<String> commonList = new LinkedList<>();
        LinkedList<String> wordList = new LinkedList<>();
        String book;
        String common;

        System.out.println(commonList.get(0));

        int[] freqList = new int[commonList.size()];

        for (int q = 0; q < 10000; q++) {
            int freq = Collections.frequency(wordList, commonList.get(q));
            freqList[q] = freq;
            System.out.println(commonList.get(q) + ": " + freqList[q]);
            //Code which simply adds freq (which holds occurance of word) into 2nd column of commonList array!
        }

    } catch (Exception e) {
        System.out.println(e);
    }
}

static String commonData(String common, String commonList) {
    try {
        Scanner sc2 = new Scanner(new File("common_words.txt"));

        while (sc2.hasNextLine()) {
            common = sc2.nextLine();

            for (String d : common.split(" ")) {
                commonList.add(d);
            }

        }
    } catch (Exception e) {
        System.out.println(e);
    }

    return commonList;
}

static String bookData(String book, String wordList) {
    try {
        Scanner sc = new Scanner(new File("alice_in_wonderland_modified.txt"));

        while (sc.hasNextLine()) {
            book = sc.nextLine();

            for (String s : book.split(" ")) {
                wordList.add(s);
            }

        }
    } catch (Exception e) {
        System.out.println(e);
    }

    return wordList;
}

}

在我在底部插入的两种方法中,单词&#34;添加&#34;带有下划线红色的错误&#34;找不到符号。符号:方法add(String)...&#34;

有人理解为什么会这样吗?谢谢你!

编辑:在1点我没有任何额外的方法,所有代码都在主方法中。为了存储需要扫描的数据+使用for循环来实际扫描数据,我在页面底部制作了2个方法(用于处理和存储数据)。出于某种原因&#34; .add&#34;仅在这些方法中加下划线红色,并且在主方法中没有用红色加下划线。

0 个答案:

没有答案