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: 73% (15 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...
-
Label the parts of this kidney and nephron. Cengage Learning
-
What is the purpose of timelines in an investigation?
-
1. Calculate the 12 ratios for James Confectioners for this year. 2. How do the ratios you calculated for this year compare to those Ivey calculated for the company last year? What factors most...
-
13. An analyst is valuing Red Inc. common stock using the dividend discount model. The company plans to start paying dividends with its first dividend of $3.25 per share occurring next year. To...
-
The following transactions pertain to Smith Training Company for Year 1: Jan. 30 Established the business when it acquired $45,000 cash from the issue of common stock. Feb. 1 Paid rent for office...
-
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",...
-
What are the basic ownership rights of common stockholders in the absence of restrictive provisions?
-
A 23 F capacitor is charged to an un- known potential Vo and then connected across an initially uncharged 13 F capacitor. If the final potential difference across the 23 uF capacitor is 32 V,...
-
Solve S ( 1 + ) (+) dt
-
Miller Venture Capital made a $5 million investment in Bavarian Sausage Technology (BST) 8 years ago and in return received 1 million shares of convertible preferred stock that can be converted into...
-
A company has 20000 employees. Some employeees take a second job to pay their bills. the company wants to know how many take a second job. They do a survey of 1600 employees out of the 20000. 7% said...
-
Sales will increase for the next quarter by the same percentage increase from the previous quarter to the last reported quarter. if sales increased 5.14 % from the last quarter to the current...
-
The inventory of Marshall's Merchandise Company was destroyed by fire on June 1. From an examination of the accounting records, the following data for the first five months of the year were obtained:...
-
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
-
In 2006, e-Design was invited unexpectedly to participate in a design competition to provide new logos and new identity for Denmark's National Sports Team, a prestige client.What should the e-Design...
-
A golfer strikes the ball from the left and the parabola is shown. The height of the parabola is 7 metres and the maximum distance horizontal distance is approximately 40 metres. The distance the...
-
When a company is bouget out and migrates data to a new company's location, what cultural aspects of the change would the new company expect or experience when working with new database...
Study smarter with the SolutionInn App