Directions: Write a program that counts the keywords in Java source code.  Revise the program in Listing 21.7.  If a keyword is in a comment or in a string, don't count it.  Pass the Java file name from the command line.  Assume the Java source code is correct and line comments and paragraph comments do not overlap. If you can show (Screenshot) of the file part(how you connect your file with java, name, etc.) I'll appreciate it because I'm struggling a lot with how to make java execute a text file

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Directions:

Write a program that counts the keywords in Java source code.  Revise the program in Listing 21.7.  If a keyword is in a comment or in a string, don't count it.  Pass the Java file name from the command line.  Assume the Java source code is correct and line comments and paragraph comments do not overlap.

If you can show (Screenshot) of the file part(how you connect your file with java, name, etc.) I'll appreciate it because I'm struggling a lot with how to make java execute a text file. 

and shuf-
ed hash set
e program
16), linked
obtains the
t (line 25),
number is
method to
her an ele-
set instead
list.
swered in
ele-
the
increas-
ments
a
fre-
data
fre-
what
LISTING 21.7 CountKeywords.java
1 import java.util. *;
2 import java.io.*;
3
4 public class CountKeywords {
10
11
12
13
14
15
+ 5 + £ ± ± 8 wwwxN2
16
17
18
19
20
21
22
23
24
25
26
27
31
29
30
28
32
34
35
7
36
8
9
33
37
6
38
39
40
5 public static void main(String[] args) throws Exception {
= new Scanner (System.in);
41
42
43
88
44
45
46
47 }
}
Scanner input
String filename = input.nextLine();
System.out.print("Enter a Java source file: ");
File file = new File(filename);
if (file.exists()) {
}
System.out.println("The number of keywords in " + filename
+ " is " + countKeywords (file));
else {
}
gives this program.
System.out.println("File" + filename + "
public static int count Keywords (File file) throws Exception {
// Array of all Java keywords + true, false and null
String[] keywordString = {"abstract", "assert", "boolean",
"break", "byte", "case", "catch", "char", "class", "const",
"continue", "default", "do", "double", "else", "enum"
"extends" "for", "final", "finally", "float", "goto",
"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", "true", "false", "null"};
Scanner input =
is a keyword. To
use the contains method to
while (input.hasNext()) {
String word =
if (keywordSet.contains (word))
count++;
Set<String> keywordSet =
new HashSet<> (Arrays.asList (keywordString));
int count = 0;
return count;
= new Scanner (file);
11
input.next();
}
does not exist");
Transcribed Image Text:and shuf- ed hash set e program 16), linked obtains the t (line 25), number is method to her an ele- set instead list. swered in ele- the increas- ments a fre- data fre- what LISTING 21.7 CountKeywords.java 1 import java.util. *; 2 import java.io.*; 3 4 public class CountKeywords { 10 11 12 13 14 15 + 5 + £ ± ± 8 wwwxN2 16 17 18 19 20 21 22 23 24 25 26 27 31 29 30 28 32 34 35 7 36 8 9 33 37 6 38 39 40 5 public static void main(String[] args) throws Exception { = new Scanner (System.in); 41 42 43 88 44 45 46 47 } } Scanner input String filename = input.nextLine(); System.out.print("Enter a Java source file: "); File file = new File(filename); if (file.exists()) { } System.out.println("The number of keywords in " + filename + " is " + countKeywords (file)); else { } gives this program. System.out.println("File" + filename + " public static int count Keywords (File file) throws Exception { // Array of all Java keywords + true, false and null String[] keywordString = {"abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "default", "do", "double", "else", "enum" "extends" "for", "final", "finally", "float", "goto", "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", "true", "false", "null"}; Scanner input = is a keyword. To use the contains method to while (input.hasNext()) { String word = if (keywordSet.contains (word)) count++; Set<String> keywordSet = new HashSet<> (Arrays.asList (keywordString)); int count = 0; return count; = new Scanner (file); 11 input.next(); } does not exist");
Expert Solution
steps

Step by step

Solved in 2 steps with 4 images

Blurred answer
Knowledge Booster
Constants and Variables
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education