Java编程-令牌“。”上的语法错误,此令牌后应@ @

问题描述 投票:0回答:2

我在System.out.println语句中的代码中收到错误。我不确定为什么在这里,代码运行正常并且似乎没有任何其他错误,它一定是程序中其他地方的代码导致了。这是错误:

令牌“。”的语法错误,此令牌后应@,期望>]

请参见下面的代码。我已将发生错误的行标记为粗体。但是,它不会停止程序的运行。我知道这里有一个类似的不同问题,但是我似乎无法根据此问题解决我的问题。 Syntax error on token ".", @ expected after this token

错误图片: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();
}

//主要方法

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

我在System.out.println语句中的代码中收到错误。我不确定为什么在这里,代码运行正常并且似乎没有任何其他错误,它一定是在...

java syntax-error
2个回答
0
投票

在您的TextProcessorCalc文件中,将您的方法包装在一个类中,如下所示:


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

}
© www.soinside.com 2019 - 2024. All rights reserved.