Rewrite Listing 21.7 CountKeywords. java to read in a Java source code file and count the occurrence
Question:
Rewrite Listing 21.7 CountKeywords. java to read in a Java source code file and count the occurrence of each keyword in the file, but don?t count the keyword if it is in a comment or in a string literal.
Listing
Transcribed Image Text:
1 import java.util.*; 2 import java.io.*; 3 4 public class Countkeywords { public static void main(String[] args) throws Exception { Scanner input = new Scanner (System.in); System.out.print("Enter a Java source file: "); String filename = input.nextline(); 8 File file = new File(filename); if (file.exists()) { System.out.println("The number of keywords in " + filename is " + countkeywords(file)); 10 11 12 13 14 else { System.out.println("File " + filename + " does not exist"); 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 public static int countkeywords (File file) throws Exception { // Array of al1 Java keywords + true, false and null String[] keywordString - {"abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "default", "do", "double", "else", "e "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"}; 'enum", 33 Set
1 import java.util.*; 2 import java.io.*; 3 4 public class Countkeywords { public static void main(String[] args) throws Exception { Scanner input = new Scanner (System.in); System.out.print("Enter a Java source file: "); String filename = input.nextline(); 8 File file = new File(filename); if (file.exists()) { System.out.println("The number of keywords in " + filename is " + countkeywords(file)); 10 11 12 13 14 else { System.out.println("File " + filename + " does not exist"); 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 public static int countkeywords (File file) throws Exception { // Array of al1 Java keywords + true, false and null String[] keywordString - {"abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "default", "do", "double", "else", "e "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"}; 'enum", 33 Set keywordSet = new HashSet<>(Arrays.asList(keywordString)); int count = 0; 34 35 36 Scanner input - new Scanner(file); 37 38 while (input.hasNext ()) { String word = input.next(); if (keywordSet.contains (word)) 39 40 41 42 count++; 43 44 45 return count; 46 47 } Enter a Java source file: c:\Welcome.java Jenter The number of keywords in c:\Welcome.java is 5 Enter a Java source file: c:\TTT.java -Enter File c:\TTT.java does not exist
Fantastic news! We've Found the answer you've been seeking!
Step by Step Answer:
Answer rating: 75% (16 reviews)
Program Plan Create a class called Count keywords Read a file name from the keyboard Check for existence of the file Define a method called Count keyw...View the full answer
Answered By
Muhammad Ghyas Asif
It is my obligation to present efficient services to my clients by providing a work of quality, unique, competent and relevant. I hope you have confidence in me and assign me the order and i promise to follow all the instructions and keep time.
4.60+
109+ Reviews
203+ Question Solved
Related Book For
Introduction to Java Programming, Comprehensive Version
ISBN: 978-0133761313
10th Edition
Authors: Y. Daniel Liang
Question Posted:
Students also viewed these Computer science questions
-
Rewrite Listing 2.10, ComputeChange .java, to fix the possible loss of accuracy when converting a double value to an int value. Enter the input as an integer whose last two digits represent the...
-
Rewrite Listing 2.10, ComputeChange. java, to fix the possible loss of accuracy when converting a float value to an int value. Read the input as a string such as "11.56". Your program should extract...
-
Rewrite Listing 21.9 to read the text from a text file. The text file is passed as a command-line argument. Words are delimited by whitespace characters, punctuation marks (,;.:?), quotation marks...
-
Rainbow manufactures wooden backyard playground equipment. Rainbow estimated $1,785,000 of manufacturing overhead and $2,100,000 of direct labour cost for the year. After the year was over, the...
-
Andria Mullins, financial manager of Webster Electronics, has been asked by the firm's CEO, Fred Weygandt, to evaluate the company's inventory control techniques and to lead a discussion of the...
-
Assume the selling prices of homes in a particular community follow a normal distribution with a mean of $285,700 and a standard deviation of $46,100. Determine the range of selling prices in this...
-
What types of job would not be suitable for each of these?
-
Elana's Traveling Veterinary Services, Inc., completed its first year of operations on December 31. All of the year's entries have been recorded except for the following: a. On March 1 of the current...
-
During April, the first production department of a process manufacturing system completed its work on 300,000 units of a product and transferred them to the next department. Of these transferred...
-
Carrie DLake, Reed A. Green, and Doug A. Divot share a passion for golf and decide to go into the golf club manufacturing business together. On January 2, 2018, DLake, Green, and Divot form the...
-
Write a program that reads an unspecified number of integers and finds the one that has the most occurrences. The input ends when the input is 0. For example, if you entered 2 3 40 3 5 4 3 3 3 2 0,...
-
Define the following methods in MyList and implement them in MyAbstractList: Write a test program that creates two MyArrayLists, list1 and list2, with the initial values {"Tom", "George", "Peter",...
-
List common ingredients used when preparing baked products, and explain the effects each has on the finished product. (pp. 229-230)
-
As the human resource manager, how would you evaluate the training needs of your staff? How can you ensure that the training you would provide is effective? What data might be used to make your...
-
MARYLAND CORPORATION manufactures three liquid products - Alpha, Beta and Gamma using a joint process with direct materials, direct labor and overhead totaling $560,000 per batch. In addition, the...
-
Three common organizational structures. Mention one organization for each organizational structure which is following a specific organizational structure. Also, provide support to your answer by...
-
You are a retail manager at Kitchen Nightmare, a relatively new store at the mall that sells mostly items for kitchens, like forks, oven mitts, etc.. You have been open since the fall of 2021 and...
-
Examine the extent to which the Department of Veteran Affairs has established any processes or procedures to ensure knowledge retention of departing employees. Why is it important to manage the...
-
Refer to Exercise 15. a. Explain why the sample results give some evidence for the alternative hypothesis. b. Calculate the standardized test statistic and P-value. c. What conclusion would you make?...
-
Danielle has an insurance policy with a premium of $75 per month. In September she is in an accident and receives a bill worth $2990 for the repair of her own property. Her deductible is $250 and her...
-
A simple parity-check bit, which is normally added at the end of the word (changing a 7-bit ASCII character to a byte), cannot detect even numbers of errors. For example, two, four, six, or eight...
-
In CRC, we have chosen the generator 1100101. What is the probability of detecting a burst error of length a. 5? b. 7? c. 10?
-
Assuming even parity, find the parity bit for each of the following data units. a. 1001011 b. 0001100 c. 1000000 d. 1110111
-
S Corporation is expanding rapidly and it currently needs to retain all of its earnings. Hence, it does not pay any dividends. However, investors expect S Corp to begin paying dividends with the...
-
What is law accordingly to John Austin ? In what sense laws are different from morality? Discuss the importance of knowing commercial law for business executives.
-
Joey purchased a 14-year T-bond with a 3.5% annual coupon four years ago at par. Today the bond's YTM 5%. If Cramer holds this bond to maturity, what internal rate of return will he earn on this...
Study smarter with the SolutionInn App