统计java源程序中关键字的个数
题目:
来自于xd Java大作业,花了我老长时间:
实现一个类 KeywordIdentifier,其中的核心方法的功能是读入一个 java 程序源文件,在控制台输出各个关键字的个数。
- 关于源文件读取的方法,请先自学教材中流试 I/O 相关的内容(7.1 节),建议逐行读取源文件内容进行分析。
- 关于关键字个数的统计,请先自学 Map 集合类的使用方法(5.6.6 节)。
- 对 Java 语言的所有关键字进行统计,不是仅针对部分关键字。
- 要求具备“跳过注释”功能,注释中出现的关键字不计入关键字个 数(要求支持单行注释“//…”和多行注释“/**/”的跳过,可不考虑 注释嵌套的情况)。
分析
- 利用RandomAccessFile读取文件(方便按行读取
- 统计关键字借助于Map类,一般用HashMap就行
- 重点就是处理注释,解决方案就是按行读取到的字符串先进行删注释的操作
- 单行注释直接利用String的replaceAll(正则表达式匹配目标,替换为空"")
- 多行注释是借助于一个变量记录读取的内容是否在注释内,即是否被/**/包裹
处理每一行的字符也是重点(这里借鉴了别人的解决方案
//利用正则先把乱七八糟的符号全部干掉
String unsigned_str = s.replaceAll("\\W", " ");
//把多个空格替换为单个
String str = unsigned_str.replaceAll(" +", " ");
//这样就直接以空格为界,可以得到由每个单词组成的字符数组
String[] str_word = str.split(" ");
代码:
立志写出最优雅的代码
import java.io.* ;
import java.util.HashMap;
import java.util.Map;
public class KeywordIdentifier {
static Map<String, Integer> keyWordCount = new HashMap<>();
public static void getKeyWord(){
String[] keywords ={"abstract","assert","boolean","break","byte","case","catch","char","class","const",
"continue","default","do","double","else","enum","extends","final","finally","float","for",
"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"};
for (String keyword : keywords) {
keyWordCount.put(keyword,0);
}
}
static boolean comment=false;
public static void keyWordCounter(String s){
if (s.contains("//")) {
s=s.replaceAll("//.+","");
}
if (s.contains("/*")){
comment=true;
}
if (comment) {
s=clearCommons(s);
}
if (s.contains("*/")) {
comment=false;
}
String unsigned_str = s.replaceAll("\\W", " ");
String str = unsigned_str.replaceAll(" +", " ");
String[] str_word = str.split(" ");
for(String word : str_word) {
if(keyWordCount.containsKey(word)) {
keyWordCount.put(word, keyWordCount.get(word)+1);
}
}
}
public static String clearCommons(String content) {
StringBuilder str=new StringBuilder(content);
if (content.contains("/*")&&content.contains("*/")) {
str.replace(content.indexOf("/*"),content.indexOf("*/"),"");
}else if (content.contains("/*")) {
int start = content.indexOf("/*");
str.replace(start,content.length(),"");
}else if (content.contains("*/")) {
int end = content.indexOf("*/");
str.replace(0,end,"");
} else {
str.replace(0,str.length(),"");
}
content=str.toString();
return content;
}
public static void main(String[] args) throws IOException {
getKeyWord();
long filePoint = 0;
String line;
RandomAccessFile file = new RandomAccessFile("src/Test.java", "r");
long fileLength = file.length();
while(filePoint < fileLength) {
line = file.readLine();
keyWordCounter(line);
filePoint = file.getFilePointer();
}
file.close();
keyWordCount.forEach((k,v) -> {
if (v>0)
System.out.println(k+":"+v);
});
}
}
测试文件
尽可能的考虑了所有刁钻的情况
public class Test {
public static void main(String[] args) {
}//return
//return
static /*static
this return
return */class test2 {
}
/*same return*/class test3{
}
}
总结
兄弟们复制的同时不要忘记留个赞