exercise18_3.java

来自「java程序设计 机械工业出版社 书籍代码」· Java 代码 · 共 51 行

JAVA
51
字号
import java.util.*;
import java.io.*;

public class Exercise18_3 {
  public static void main(String[] args) {
    // Check usage
    if (args.length != 1) {
      System.out.println("Usage: java Exercise18_3 file.java");
      System.exit(0);
    }

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

    Set keywordSet = new HashSet(Arrays.asList(keywordString));
    int count = 0;
    BufferedReader in = null;

    try {
      in = new BufferedReader(new FileReader(args[0]));

      String s = null;
      while ( (s=in.readLine()) != null) {
        // Extract tokens
        StringTokenizer st = new StringTokenizer(s);

        while (st.hasMoreTokens()) {
          String token = st.nextToken();
          if (keywordSet.contains(token))
            count++;
        }
      }

      System.out.println("The number of keywords in the program is "
        + count);
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?