0

Please help me get this work :(
(My objective is to count words in a file from input.txt file.)

I get 8 compiling errors in the following code. (All cannot find symbol) Compiler points out:

  1. The letter "P" in "Paths.get".
  2. The letter "F" in "Files.readAllLines"
  3. The symbol "." in "titleList.add(line)"
  4. The symbol "." in "titleList.get"
  5. The symbol "." in "st.add(filterList);"
  6. The symbol "." in "filterList.removelAll(stopWordsArray);"
  7. The letter "n" in "Map map = new Map< String, Integer>();"
  8. The symbol "." in "for (int i =0; i < map.length && i < 20; i++)"

Lines of code with errors are bold.

Any idea what is wrong with my code? I'm a beginer with java and help is greatly aprecited.

  public String[] process() throws Exception {
    String[] ret = new String[20];

    //TODO
    // Pass user id
    initialRandomGenerator(this.userName);

    //  Get the index into array
    Integer[] indexes = this.getIndexes();

    // Create Array to store input.txt
    String[] titleList = new String[10000];

    // Put each line of input.txt in Array

    // ERRORS HERE
    **for (String line : Files.readAllLines(Paths.get(this.inputFileName))){
        titleList.add(line);
    }**
    // Create array to store list of words to be proccess
    String[] filterList = new String[50000];

    // Look for words in file
     for (int i = 0; i < indexes.length; i++){
        // Tokennize, lower case, trim and stop delimiter.



         // ERRORS HERE 
         **StringTokenizer st = new StringTokenizer(titleList.get(indexes[i]).trim().toLowerCase(), this.delimiters);** 
        // Add word to Filter list array
         **st.add(filterList);**
     }

     // Remove stopWords from filter list array.
     // ERRORS HERE
     **filterList.removelAll(stopWordsArray);**      

     //  Declaring the map to count
    // ERRORS HERE 
    **Map<String, Integer> map = new Map< String, Integer>();**

    // Loop to count
    for (int i = 0; i < filterList.length; i++ ){   
        // Get the word
        String word = filterList[i];

        //Count the word
        if (map.get(word) != null) {
            // another occurence of an existing
            // word
            int count = map.get(word);
            count++;
            map.put(word, count);
        } else {
            // first occurence of this word
            map.put(word, 1);
        } 
    }
    // Sort the list by frequency in a descending order. Use sort function.
    // map.collections.sort( list, new Comparator<Map.Entry<word, count>>();


    // Display first 20 words.
    // ERRORS HERE        
    **for (int i =0; i < map.length && i < 20; i++){
        System.out.println(filterList[i]);**
    }

    return ret;
}
David
  • 23
  • 7
  • 1
    There are so many errors in your code, you can't expect us to fix all of them for you. Try using an IDE (I'm guessing you are not), for example [eclipse](https://eclipse.org/), it will tell you what's wrong with your code. Or at least most of what's wrong with your code. – User42 Sep 02 '15 at 13:44
  • I don't expect you to fix them for me. Maybe you could point out one and tell me what you think. I know there are errors. They are listed upfront. Anyway thanks for not helping at all. – David Sep 02 '15 at 13:57
  • What happened to the answer that was given? Someone already pointed out 4 general mistakes. What I was trying to contribute, was that these are multiple, different, standard mistakes. I can understand that the errors you get are not very helpful, that's why I recommend using an IDE such as Eclipse (you didn't say if you are or not). There, you get more precise error messages, with additional information to the problem. If this still doesn't help, you can google these and will find many questions and answers, I'm sure you could find a solution this way. – User42 Sep 02 '15 at 14:05
  • As the answer I just mentioned seems to be deleted, here are some hints: You cannot instantiate `Map`, use `HashMap` instead. `.length` can only be called for arrays, you are trying to call it on a `Map`, use `size()` instead. You are trying to use methods of `Collection` (`get`, `add`, `removelAll`/ `removeAll` here is even a typo) on arrays, which are not `Collections`. You have to decide if you want to use these methods and work with `Collections` or stay with your arrays and do these things manually. – User42 Sep 02 '15 at 14:14
  • I did not see who pointed out 4 general mistakes. I'm using Eclipse. This class can't be debug for some strange reason I don't understand. I'm a beginer. I'll ask google. – David Sep 02 '15 at 14:16
  • Thank you for the hints. They are greatly aprecieted. – David Sep 02 '15 at 14:19
  • Okay. Then probably something else is broken in your class. It will likely help you most, if you find out what it is. You don't even have to debug normally, the compiler warnings should give you more information. If you post the rest of your code, i.e. the whole class, we might be able to help you with this (but please, just if the code is not too long). Or have a look at [this](http://stackoverflow.com/a/25706217/1638708), it tells you a lot about the `Cannot find symbol` compilation error. – User42 Sep 02 '15 at 14:27
  • Thanks. One question: I'm using: "String[] filterList = new String[50000];" to create an array. As you mentioned, this array is not collection so .add .removeAll will not work. How do I make the array filterList a collection? – David Sep 02 '15 at 15:56
  • Have you found out yet? If not, look [here](http://stackoverflow.com/a/4255054/1638708) – User42 Sep 09 '15 at 07:47
  • Yes I have. Please see answer below. Best regards. – David Sep 09 '15 at 09:36

1 Answers1

1

Ok, so I got it t work. It was a bit more difficult because the file given could not be debug with the IDE. The issues were pretty simple.

I get 8 compiling errors in the following code. (All cannot find symbol) Compiler points out:

1- The letter "P" in "Paths.get". (I needed to import library: import java.nio.file.Paths;)

2- The letter "F" in "Files.readAllLines" (I needed to import library: import java.nio.file.Files;)

3- The symbol "." in "titleList.add(line)" (The method .add is only available in collections, e.g. ArrayList I was trying to use it in an Array. )

4- The symbol "." in "titleList.get"

(The method .get is only available in collections, e.g. ArrayList I was trying to use it in an Array. )

5- The symbol "." in "st.add(filterList);"

(The method .add is only available in collections, e.g. ArrayList I was trying to use it in a StringTokenizer )

6- The symbol "." in "filterList.removelAll(stopWordsArray);"

(The method .get is only available in collections, e.g. ArrayList I was trying to use it in an Array. )

7- The letter "n" in "Map map = new Map< String, Integer>();"

(Map needed to be initialise to be a type of Map. Not only Map. E.g. HashMap)

8- The symbol "." in "for (int i =0; i < map.length && i < 20; i++)"

(Length is not a property of maps; Size must be use instead.)

Kind regards

David
  • 23
  • 7