Question: Fix this code so it is actually able to read a file from a cs server, right now it just errors. _ _ _ _

Fix this code so it is actually able to read a file from a cs server, right now it just errors.
__________________________
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 server file path (e.g.,/server/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);
}
private static void processFile(String filePath, boolean includeNoiseWords){
TreeMap> map = new TreeMap<>();
ScannerWithLineno swl = null;
try {
swl = new ScannerWithLineno(new File(filePath));
while (swl.hasNext()){
String word = swl.next();
int lineNumber = swl.getLineno();
if (!includeNoiseWords && isNoiseWord(word)){
continue;
}
if (!map.containsKey(word)){
map.put(word, new LinkedHashSet<>());
}
map.get(word).add(String.valueOf(lineNumber));
}
printCrossReference(map);
swl.close();
} catch (FileNotFoundException e){
e.printStackTrace();
}
}
private static void printCrossReference(TreeMap> map){
Set keys = map.keySet();
for (String key : keys){
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");
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!