Revise the program in Listing 21.7. If a keyword is in a comment or in a string,
Question:
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 that the Java source code is correct and line comments and paragraph comments do not overlap.
Listing
Transcribed Image Text:
1 import java.util.*; 2 import java.io.*; 3 4 public class Countkeywords { 5 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(); 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 33 34 35 36 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", "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"}; Set
1 import java.util.*; 2 import java.io.*; 3 4 public class Countkeywords { 5 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(); 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 33 34 35 36 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", "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"}; Set keywordSet = new HashSet<> (Arrays.asList(keywordString)); int count = 0; 37 Scanner input = new Scanner(file); 38 while (input.hasNext ()) { String word = input.next(); if (keywordSet.contains(word)) count++; 39 40 41 42 43 44 45 return count; 46 47 } Nmm mmm mmm3 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: 55% (9 reviews)
Output The nuffber of keywords 50 Data in keywordstxt throw continue interface throws default long t...View the full answer
Answered By
PALASH JHANWAR
I am a Chartered Accountant with AIR 45 in CA - IPCC. I am a Merit Holder ( B.Com ). The following is my educational details.
PLEASE ACCESS MY RESUME FROM THE FOLLOWING LINK: https://drive.google.com/file/d/1hYR1uch-ff6MRC_cDB07K6VqY9kQ3SFL/view?usp=sharing
3.80+
3+ Reviews
10+ 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
-
The program in Listing 3.1, AdditionQuiz.java, generates two integers and prompts the user to enter the sum of these two integers. Revise the program to generate three single-digit integers and...
-
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...
-
Write a program similar to Listing 7.9, Calculator.java. Instead of using integers, use rationals, as shown in Figure 13.10a. You will need to use the split method in the String class, introduced in...
-
Develop a two-period weighted moving average forecast for periods 12 through 15. Use weights of 0.7 and 0.3, with the most recent observation weighted higher. PERIOD DEMAND 10............ 248...
-
What is the linear hull of the vectors {(1, 0), (0,2)} in R2?
-
The following facts apply to two companies for the year ending December 31 of the same year: No long-term debt (principal) payments are due for either firm for 10 years. Before evaluating the...
-
Reconsider the data from Problem 51. What is the capital recovery cost of Alternative 3 for a 6-year life? Data from problem 51 Alternatives 1, 2, and 3 have lives of 3, 4, and 6 years, respectively....
-
Bowman Builders manufactures steel storage sheds for commercial use. Joe Bowman, president of Bowman Builders, is contemplating producing sheds for home use. The activities necessary to build an...
-
When did 3D printing start in your selected industry? How was it first used in this industry?
-
Arne Adams, the audit senior, is reviewing the working papers written by the audit staff on the audit of Virginia Creepers, a garden nursery and retailer of garden accessories. Arne reads the...
-
CountOccurrenceOfWords.java) Rewrite Listing 21.9 to display the words in ascending order of occurrence counts. Listing 1 import java.util.*; 2 3 public class CountOccurrence0fWords { 4 public static...
-
Write a program that reads words from a text file and displays all the nonduplicate words in ascending order. The text file is passed as a command-line argument.
-
If economic profit equals accounting profit, what do implicit costs equal?
-
Initial Conditions In physics, there are special states that we care about. One of these is the initial conditions of a problem. This is just the answer to the question "what are the position and...
-
Assume that the Oakley Corporation has fixed costs of $100,000 and variable costs of $100 per unit. Which selling prices will yield the maximum net income?
-
Katherine owns several apartment buildings in corvallis, oregon, that she rents out to university students. she uses part of the rental income to pay for insurance premiums. how should katherine...
-
Baker v. Carr (1962) saw the Supreme Court enter a "political thicket" that it had been careful to avoid in the past. However, the politics of the decision extend beyond the fact that it dealt with...
-
Eloise Corp. produces an unfinished toy, which currently sells for $60. To produce this unfinished toy, it costs Eloise Corp. $20. Recently, Eloise thought about continuing processing the toy by...
-
Refer to the financial statements and Notes to Consolidated Financial Statements for Reitmans (Canada) Limited in Appendix A. Instructions (a) How does Reitmans value its inventory? (b) Which...
-
Write the general quadratic equation y2 - 8y - 4x + 28 = 0 in standard form. Determine the vertex, focus, and directrix of the parabola defined by this equation. Sketch a graph.
-
What is the relationship between period and frequency?
-
If there is a single path between the source host and the destination host, do we need a router between the two hosts?
-
Assume we want to connect two isolated hosts together to let each host communicate with the other. Do we need a link-layer switch between the two? Explain.
-
A function is given. r(t) = 5 - t; t = 5, t = 10 (a) Determine the net change between the given values of the variable. (b) Determine the average rate of change between the given values of the...
-
Why are certain functional dependencies called trivial functional dependencies? Explain what is meant by repetition of information and inability to represent information. Explain why each of these...
-
When is a table in BCNF? Explain what this means and provide a specific example? What is a surrogate key, and when should you use one? Give specific examples ? What is normalization? Why is it...
Study smarter with the SolutionInn App