Any idea how to "not" count the keywords in the set if it is in a comment or string?
<p>I need to make this program count the keywords in .java source code files(which it does), but it also needs to disgregard the keywords that are in any comments or in strings. Any help would be greatly appreciated.!!!</p>

<pre><code>import java.util.*;

import java.io.*;

public class CountKeywords {

public static void main(String[] args) throws Exception {

Scanner input = new Scanner(System.in);
System.out.print("Enter a Java source file: ");
String filename = input.nextLine();

File file = new File(filename);
if (file.exists()) {
  System.out.println("The number of keywords in " + filename
    + " is " + countKeywords(file));
}
else {
  System.out.println("File " + filename + " does not exist");
}
}

public static int countKeywords(File file) throws Exception {

// Array of all Java keywords + true, false and null

String[] keywordString = {"abstract", "assert", "boolean",
    "break", "byte", "case", "catch", "char", "class", "const",
    "continue", "default", "do", "double", "else", "enum",
    "extends", "for", "final", "finally", "float", "goto",
    "if", "implements", "import", "instanceof", "int",
    "interface", "long", "native", "new", "package", "private",
    "protected", "public", "return", "short", "static",
    "strictfp", "super", "switch", "synchronized", "this",
    "throw", "throws", "transient", "try", "void", "volatile",
    "while", "true", "false", "null"};

Set&lt;String&gt; keywordSet =
  new HashSet&lt;String&gt;(Arrays.asList(keywordString));
int count = 0;

Scanner input = new Scanner(file);

while (input.hasNext()) {
  String word = input.next();
  if (keywordSet.contains(word))
    count++;
}

return count;
}
}
</code></pre>
