📄 exercise18_3.java
字号:
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -