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
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
Related questions
Question
Directions:
Write a
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");](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fbdc33a51-efff-4610-b226-be7150e1db8b%2F90ea3dec-2c04-45a1-bba7-d35c9b558177%2F5i7mmth_processed.jpeg&w=3840&q=75)
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

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 2 steps with 4 images

Knowledge Booster
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.Recommended textbooks for you

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)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON

Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON

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)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON

Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON

C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON

Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning

Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education