0

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

Image of errors: Errors

//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();   
    }
}
Royston
  • 11
  • 7
  • 2
    Can you edit your question to include the entire stack trace? Or at least indicate what line of code this error is occurring on? – Green Cloak Guy Feb 25 '20 at 13:17
  • What are with these stars in the system out print line? I mean why `**System.out.println` – Royal Bg Feb 25 '20 at 13:18
  • 2
    He is trying to format it bold. – Michele Dorigatti Feb 25 '20 at 13:19
  • In which file and class is the method `fileAnalysis` ? – jhamon Feb 25 '20 at 13:19
  • 1
    @GreenCloakGuy This is a compile-time error, so there is no stack trace. But yes, the OP should include the full error message. – kaya3 Feb 25 '20 at 13:20
  • The Markdown **bold** will not work inside of a code-snippet. Instead, perhaps you can add a comment to the end of the line? – sdgfsdh Feb 25 '20 at 13:21
  • @jhamon -`fileAnalysis` is name of the first method posted in the question. – Arvind Kumar Avinash Feb 25 '20 at 13:21
  • @CoderLee Please do not edit the code in the question to fix misspellings in the code; the problem may be *caused* by those misspellings, in which case your edit makes the question meaningless. – kaya3 Feb 25 '20 at 13:22
  • @jhamon I'm not editing it now – CoderLee Feb 25 '20 at 13:22
  • @ArvindKumarAvinash I get that. But my question is more: is it in the same class than `TextProcessorUsingHashmaps` because in the provided code, there is only the `main`method in this class. So is it defind in this class or above the class declaration/improts – jhamon Feb 25 '20 at 13:23
  • @Royston Please include error messages as text, not as images. But you have a lot of syntax errors, not just the one in your title, so perhaps the problem is that you didn't write your `fileAnalysis` method in a class? – kaya3 Feb 25 '20 at 13:26
  • @Royston I can not reproduce your problem with Intellij and java13, the code compiles and runs (after I removed the broken ** ** formatting try). What IDE are you using? Maybe the problem lies there? – lugiorgi Feb 25 '20 at 13:32
  • Hi there, the fileAnalysis method is in a class, and the main method is in a separate class. This line is causing the issues: System.out.println("Please enter the filepath: "); I am using Visual Studio Code – Royston Feb 25 '20 at 13:34
  • There is no way it's in a different class and you run it from your main simply with `fileAnalysis();`. Either there is a missing part or you made some major change without saving/building your project – jhamon Feb 25 '20 at 13:55
  • 1
    You cannot define a method not belonging to a class. Put this whole code inside a class in order to fix this. – MC Emperor Feb 25 '20 at 14:38

2 Answers2

0
package com.test;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class CheckTest {

  public static void main(String args[]) throws IOException {

    //Method for analysing filepath

    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();
  }

  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();
  }

}
0

In your TextProcessorCalc file, wrap your methods inside a Class like so:

public class TextProcessorMain {  YOUR METHODS }

Then from your TextProcessorMain file, call the methods using a full stop preceded by TextProcessor Calc (the class), as below:

 if (modeChoice.equals ("demo")){
      TextProcessorCalc.demo();
    }

That should solve the errors.

Charlie
  • 230
  • 1
  • 9