Question: Write a program that reads a Java source-code file and reports the number of keywords (including null , true , and false ) in the
Write a program that reads a Java source-code file and reports the number of keywords (including null, true, and false) in the file. If a keyword is in a comment or in a string, dont count it. Test your program to count the keywords in the Welcome.java file:
Please input the Java filename: Welcome.java The number of keywords in the program is 6
Hint: Create a set to store all the Java keywords:
Here is the code:
import java.util.*;
public class HW3_Q1 {
public static void main(String[] args) {
// Array of all Java keywords + true + null
String[] keywordString = { "abstract", "finally", "public", "boolean", "float",
"return", "break", "for", "short", "byte",
"goto", "static", "case", "if", "super", "catch",
"implements", "switch", "char", "import", "synchronized",
"class", "instanceof", "this", "const", "int",
"throw", "continue", "interface", "throws", "default",
"long", "transient", "do", "native", "try",
"double", "new", "void", "else", "package",
"volatile", "extends", "private", "while", "final",
"protected", "true", "null"
};
Scanner scanner = new Scanner(System.in);
System.out.print("Please input the Java filename: ");
String filename = scanner.nextLine();
scanner.close();
// Your code here
// ...
}
// Or here
// ...
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
