Question: Edit this code so that it is able to read a file from an online cs server and change the noise word checker so that

Edit this code so that it is able to read a file from an online cs server and change the noise word checker so that it uses a BST.
___________________________
import java.io.File;
import java.io.FileNotFoundException;
import java.util.LinkedHashSet;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
public class WordCrossReference2{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Enter the file path (e.g.,/path/to/file.txt): ");
String filePath = in.nextLine();
System.out.print("Include noise words in output (yes/no)?");
boolean includeNoiseWords = in.nextLine().equalsIgnoreCase("yes");
processFile(filePath, includeNoiseWords);
in.close();
}
private static void processFile(String filePath, boolean includeNoiseWords){
TreeMap> map = new TreeMap<>();
try (Scanner scanner = new Scanner(new File(filePath))){
int lineNumber =1;
// Read file line by line
while (scanner.hasNextLine()){
String line = scanner.nextLine();
String[] words = line.split("\\W+"); // Split line into words based on non-word characters
for (String word : words){
if (word.isEmpty()) continue; // Skip empty words
if (!includeNoiseWords && isNoiseWord(word)){
continue; // Skip noise words if not included
}
word = word.toLowerCase(); // Normalize to lowercase
// Add word to map and record line number
map.putIfAbsent(word, new LinkedHashSet<>());
map.get(word).add(lineNumber);
}
lineNumber++; // Increment line number
}
// Print the cross-reference
printCrossReference(map);
} catch (FileNotFoundException e){
System.err.println("Error: File not found at "+ filePath);
}
}
private static void printCrossReference(TreeMap> map){
for (String key : map.keySet()){
System.out.println(key +" : "+ map.get(key));
}
}
private static boolean isNoiseWord(String word){
Set noiseWords = Set.of(
"the", "and", "a","an","of","to","in", "for", "on","at", "name", "very"
);
return noiseWords.contains(word.toLowerCase());
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!