Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd92f40c

Browse files
author
jsquared21
committed
Add Ex 21.3
1 parent2e0a01a commitd92f40c

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed
2.69 KB
Binary file not shown.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*********************************************************************************
2+
* (Count the keywords in Java source code) Revise the program in Listing 21.7. *
3+
* If a keyword is in a comment or in a string, don’t count it. Pass the Java *
4+
* file name from the command line. Assume that the Java source code is correct *
5+
* and line comments and paragraph comments do not overlap. *
6+
*********************************************************************************/
7+
importjava.util.*;
8+
importjava.io.*;
9+
10+
publicclassExercise_21_03 {
11+
publicstaticvoidmain(String[]args)throwsException {
12+
// Check command line length
13+
if (args.length !=1) {
14+
System.out.println("Usage: java sourceFile");
15+
System.exit(1);
16+
}
17+
18+
// Check if file exists and display keyword count
19+
Filefile =newFile(args[0]);
20+
if (file.exists()) {
21+
System.out.println("The number of keywords in " +args[0]
22+
+" is " +countKeywords(file));
23+
}
24+
else {
25+
System.out.println("File " +args[0] +" does not exist");
26+
}
27+
}
28+
29+
/** Method returns the keyword count */
30+
publicstaticintcountKeywords(Filefile)throwsException {
31+
// Array of all Java Keywords + ture, false and null
32+
String[]keywordString = {"abstract","assert","boolean",
33+
"break","byte","case","catch","char","class","const",
34+
"continue","default","do","double","else","enum",
35+
"extends","for","final","finally","float","goto",
36+
"if","implements","import","instanceof","int",
37+
"interface","long","native","new","package","private",
38+
"protected","public","return","short","static",
39+
"strictfp","super","switch","synchronized","this",
40+
"throw","throws","transient","try","void","volatile",
41+
"while","true","false","null"};
42+
43+
Set<String>keywordSet =
44+
newHashSet<>(Arrays.asList(keywordString));
45+
intcount =0;
46+
47+
Scannerinput =newScanner(file);
48+
49+
while (input.hasNext()) {
50+
Stringword =input.next();
51+
if (word.equals("//")) {// Don't count comments
52+
input.nextLine();
53+
}
54+
elseif (word.contains("\"")) {// Don't count string
55+
StringnextWord;
56+
do {
57+
nextWord =input.next();
58+
}while (!nextWord.contains("\""));
59+
}
60+
elseif (word.contains("/*")) {// Don't count block comments
61+
StringnextWord;
62+
do {
63+
nextWord =input.next();
64+
}while (!nextWord.contains("*/"));
65+
}
66+
elseif (keywordSet.contains(word))
67+
count++;
68+
}
69+
70+
returncount;
71+
}
72+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp