62,614
社区成员
发帖
与我相关
我的任务
分享
Map<String, Integer> javaKeys = new HashMap<String, Integer>();
javaKeys.put("public", 0);
javaKeys.put("import", 0);
javaKeys.put("package", 0);
javaKeys.put("void", 0);
javaKeys.put("class", 0);
javaKeys.put("static", 0);
try {
BufferedReader reader = new BufferedReader(new FileReader(new File("Test.java")));
String line = reader.readLine();
while(line != null) {
String[] words= line.split("[ \t.();:]");
for (String word : words) {
System.out.println(word);
if (javaKeys.containsKey(word))
{
int count = javaKeys.get(word);
count++;
javaKeys.put(word, count);
}
}
line = reader.readLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}