I am getting an error in my code on a System.out.println statement. I'm not sure why this is here, the code runs fine and doesn't seem to have any other errors, it must be some code elsewhere in the progrma that is causing it. This is the error:
Syntax error on token ".", @ expected after this token
Please see the below code. I have marked the line in bold where the error occurs. It doesn't stop the program running, however. I know there is a different question on here that is similar, but I can't seem to figure out my problem based on it. Syntax error on token ".", @ expected after this token
//Method for analysing filepath
public static void fileAnalysis() throws IOException {
// read in filepath
System.out.println("Please enter the filepath: ");
Scanner fileInput = new Scanner(System.in);
String filepath = fileInput.nextLine();
// read in file
Scanner textToBeRead = new Scanner(new FileInputStream(filepath));
Path path = Paths.get(filepath);
String fileContents = Files.readAllLines(path).toString();
fileContents = fileContents.replace('!', ' ').replace('?', ' ').replace('.',' ').replace(',',' ')
.replace(':',' ').replace(';',' ').replace('(',' ').replace(')',' ');
String[] wordArray = fileContents.split(" ");
// Create a HashMap object to contain words and times they occur
Map <String, Integer> wordFrequencies = new HashMap <String, Integer> ();
//splits text into array of words
// Initialize frequency table from text file
for (String a : wordArray)
{
String _a = a.toLowerCase();
//If _a contains '\''
//Then
//If '\'' is at the very beginning or very end, Then get rid of it
//Else keep it
Integer freq = wordFrequencies.get(_a);
wordFrequencies.put(_a, (freq == null) ? 1 : freq + 1);
}
System.out.println (wordFrequencies.size() + " distinct words:");
System.out.println (wordFrequencies);
fileInput.close();
textToBeRead.close();
}
//MAIN METHOD
import java.io.FileInputStream; import java.io.IOException; import
java.nio.charset.StandardCharsets; import java.nio.file.Files; import
java.nio.file.Path; import java.nio.file.Paths; import
java.util.HashMap; import java.util.List; import java.util.Map; import
java.util.Scanner;
public class TextProcessorUsingHashmaps {
public static void main(String[] args) throws IOException {
System.out.println("Please choose run mode: demo , file , text");
Scanner mode = new Scanner (System.in);
String modeChoice = mode.nextLine();
if (modeChoice.equals ("file")){
fileAnalysis();
}
else{
System.out.println("Invalid. Please select a valid mode.");
}
mode.close();
}
}