Lecture Videos
  1  import java.util.*;
  2  import java.io.*;
  3  
  4  public class CountKeywords {
  5    public static void main(String[] args) throws Exception {  
  6      Scanner input = new Scanner(System.in);
  7      System.out.print("Enter a Java source file: ");
  8      String filename = input.nextLine();
  9  
 10      File file = new File(filename);
 11      if (file.exists()) {
 12        System.out.println("The number of keywords in " + filename 
 13          + " is " + countKeywords(file));
 14      }
 15      else {
 16        System.out.println("File " + filename + " does not exist");
 17      }    
 18    }
 19  
 20    public static int countKeywords(File file) throws Exception {  
 21      // Array of all Java keywords + true, false and null
 22      String[] keywordString = {"abstract", "assert", "boolean", 
 23        "break", "byte", "case", "catch", "char", "class", "const",
 24        "continue", "default", "do", "double", "else", "enum",
 25        "extends", "for", "final", "finally", "float", "goto",
 26        "if", "implements", "import", "instanceof", "int", 
 27        "interface", "long", "native", "new", "package", "private",
 28        "protected", "public", "return", "short", "static", 
 29        "strictfp", "super", "switch", "synchronized", "this",
 30        "throw", "throws", "transient", "try", "void", "volatile",
 31        "while", "true", "false", "null"};
 32  
 33      Set<String> keywordSet = 
 34        new HashSet<>(Arrays.asList(keywordString));
 35      int count = 0;    
 36  
 37      Scanner input = new Scanner(file);
 38  
 39      while (input.hasNext()) {
 40        String word = input.next();
 41        if (keywordSet.contains(word)) // Test if word is a keyword
 42          count++;
 43      }
 44  
 45      return count;
 46    }
 47  }