Write a program that reads a Java source-code file and reports the number of key
ID: 3905353 • Letter: W
Question
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, don’t count it. Test your program to count the keywords in the Welcome.java file:
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
// ...
}
Explanation / Answer
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks
// HW3_Q1.java
import java.io.File;
import java.io.FileNotFoundException;
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"
};
/**
* creating a set using all keywords
*/
Set<String> keywordSet = new HashSet<String>(
Arrays.asList(keywordString));
Scanner scanner = new Scanner(System.in);
System.out.print("Please input the Java filename: ");
String filename = scanner.nextLine();
scanner.close();
try {
/**
* opening file
*/
scanner = new Scanner(new File(filename));
// denotes if the line should be ignored as a comment
boolean ignore = false;
// count of keywords
int count = 0;
String line = "";
/**
* fetching line by line
*/
while (scanner.hasNext()) {
/**
* Extracting the line, and removing trailing/leading spaces
*/
line = scanner.nextLine().trim();
if (line.startsWith("/*")) {
// start of multi line comment, ignoring
ignore = true;
}
/**
* checking if the line should be ignored or not
*/
if (!ignore) {
/**
* Defining another scanner for reading words from current
* line
*/
Scanner lineScanner = new Scanner(line);
/**
* splitting the line using white space, dot, paranthesis,
* or semi colon (any one of them is enough)
* */
lineScanner.useDelimiter(" |\.|\(|\)|\{|\}|\;");
/**
* looping through all tokens
* */
while (lineScanner.hasNext()) {
String item = lineScanner.next();
if (item.contains("//")) {
//comment, remaining part of the line is ignored
break;
}
if (keywordSet.contains(item)) {
//found
count++;
}
}
}
if (line.endsWith("*/")) {
//end of multi line comment
ignore = false;
}
}
System.out.println("The number of keywords in the program is "
+ count);
} catch (FileNotFoundException e) {
System.out.println(e);
}
}
}
/*OUTPUT*/
Please input the Java filename: Test.java
The number of keywords in the program is 9
/*Test.java (used input file)*/
package pkg;
public class Test {
static int N=500;//int n=500
/**
* @param args
* @return nothing
*/
public static void main(String[] args) {
/**
* Displaying int N
*/
System.out.println(N);
/**
* float M=N+5;
*/
float M=N+5;//float m
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.