[Java] Please test your code in the link I provide before you post your answer.
ID: 3835668 • Letter: #
Question
[Java] Please test your code in the link I provide before you post your answer.
The output should be looked like exact same as the tester.
http://www.codecheck.it/files/170405164364q7baywqni9maqzcfwf57iex
Thank you.
Write an application IndexMaker which reads a simplified Java source file and produces an index containing identifiers . Print all the identifiers and all the line numbers on which each identifier occurs. But do not print reserved keywords. When you find an identifier, compare it against a list of reserved keywords and skip keywords. Be smart about this and do not use 50 if statements. Put the keywords in a data structure and check each identifier to see if it is in the collection. Here is a file of reserved keywords. Download it and put in your project.
Print the identifiers in lexicographical order. Display the line numbers in numeric order.
We will assume
every string consisting of only letters, numbers, and underscores is either an identifier or reserved keyword. But we are ignoring keywords
there are no comments to confuse things
You will need to set the delimiter: ...useDelimiter("[^A-Za-z_]+"); For simplicity, there will not be any identifiers that contain numbers.
Your output should be in this format:
ProcessRectangle: [4]
Rectangle: [2, 9, 11]
String: [7]
System: [13, 16]
args: [7]
You can simply declare that the main method throws the FileNotFoundException. You do not need to catch it.
Here are the Java files that are used: ProcessRectangle.java and PaintJobCalculator.java. CodeCheck will process both files.
Use this line in your code for the file name
String filename = "ProcessRectangle.java"; //SUB "PaintJobCalculator.java"
Call the reserved keyword file reserved.txt
***************File of reserved keyword******************
**************************************************
*********ProcessRectangle.java******************
**************************************************
*********PaintJobCalculator.java*****************
***************************************************
Explanation / Answer
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;
public class IndexMaker {
public static String KEYWORDFILENAME = ""; // provide your keywords file path
public static String JAVAFILEPATH = ""; // provide your java filepath
public static void main(String[] args) throws FileNotFoundException, IOException {
BufferedReader bufferedReader = null;
BufferedReader filebufferedReader = null;
String currentLine = "";
int lineNo = 0;
List<String> keyWordsList = new ArrayList();
TreeMap<String, String> identifierIndexes = new TreeMap<String, String>(); /*As treeMap is sorted, here we use treeMap for lexicographical order */
bufferedReader = new BufferedReader(new FileReader(KEYWORDFILENAME));
filebufferedReader = new BufferedReader(new FileReader(JAVAFILEPATH));
while ((currentLine = bufferedReader.readLine()) != null) {
keyWordsList.add(currentLine);
}
while ((currentLine = filebufferedReader.readLine()) != null) {
lineNo++;
String[] split = currentLine.split("[^A-Za-z_]+");
for (int i = 0; i < split.length; i++) {
if (!"".equalsIgnoreCase(split[i]) && !keyWordsList.contains(split[i])) {
if (identifierIndexes.containsKey(split[i])) {
identifierIndexes.put(split[i], identifierIndexes.get(split[i]) + lineNo + ",");
} else {
identifierIndexes.put(split[i], lineNo + ",");
}
}
}
}
identifierIndexes.keySet().stream().forEach((String name) -> { // lamda expressions for iterating treeMap
String identifier = name;
String temp = identifierIndexes.get(name);
String occrrences = "[" + temp.substring(0, temp.lastIndexOf(",")) + "]";
System.out.println(identifier + " : " + occrrences);
});
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.