0

Im trying to create a program which takes a list of common words, scans a book and counts the occurance of each word! Everything is going good, but:

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

}

Within the 2 methods I inserted at the bottom, the word "add" is underlined red with the error "cannot find symbol. Symbol: method add(String) ... "

Does anybody understand why this is happening? Thanks for anything!

EDIT: At 1 point I didn't have any extra methods and had all the code sit in the main method. In order to store the data that needs to be scanned + use a for loop to actually scan the data, I made 2 methods at the bottom of the page (to process and store data). For some reason ".add" is underlined red only when in these methods, and isn't underlined red when in the main method.

James
  • 47
  • 7
  • 1
    You should try to compile this and list (edit your question) the exact errors you are getting. Nobody can see what's underlined red on your screen. – pvg Mar 09 '17 at 22:28
  • 1
    You have wordList defined as a `String`. Did you want it to be a `List` of some sort? – D M Mar 09 '17 at 22:29
  • If you are getting a red underline for the `add` method then it's possible you have no imported the proper classes. – CJ Jacobs Mar 09 '17 at 22:32
  • 3
    This is because there is no `add` method in the `String` class. – Joe C Mar 09 '17 at 22:36
  • Just a note: At 1 point I didn't have any extra methods and had all the code sit in the main method. In order to store the data that needs to be scanned + use a for loop to actually scan the data, I made 2 methods at the bottom of the page. For some reason ".add" is underlined red only when in these methods, and isn't underlined red when in the main method. – James Mar 09 '17 at 22:48
  • in `main` you have `LinkedList wordList = new LinkedList<>();`. In `bookData` you have `String wordList`. That's why they aren't behaving the same. One's a list of Strings, and one is just a String. – D M Mar 09 '17 at 23:50
  • As Joe C said, I had to change the strings to LinkedLists Strings for this code to works, thanks all! – James Mar 14 '17 at 16:53

0 Answers0