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
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

![5 public static void main(String[] args) throws Exception { Scanner input =](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2022/11/636a73bd937a5_829636a73bd834c0.jpg)
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
Step by Step Solution
3.37 Rating (172 Votes )
There are 3 Steps involved in it
Output The nuffber of keywords 50 Data in keywordstxt throw continue interface throws default long t... View full answer
Get step-by-step solutions from verified subject matter experts
