Search code examples
javacompiler-errorsword-count

Java word counts: Error: Cannot find symbol. Java compiler


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

Solution

  • 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