gpt4 book ai didi

Java 编程 — 标记 "."出现语法错误,@ 在此标记之后

转载 作者:行者123 更新时间:2023-12-01 18:14:50 24 4
gpt4 key购买 nike

我的代码中的 System.out.println 语句出现错误。我不确定为什么会出现这种情况,代码运行良好并且似乎没有任何其他错误,它一定是程序中其他地方的某些代码导致了它。这是错误:

Syntax error on token ".", @ expected after this token

请看下面的代码。我已经用粗体标记了发生错误的行。但是,它不会停止程序运行。我知道这里有一个类似的不同问题,但我似乎无法根据它找出我的问题。 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();
}
}

最佳答案

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

public class TextProcessorMain {  YOUR METHODS }

然后从 TextProcessorMain 文件中,使用句号并在 TextProcessor Calc(类)之前调用方法,如下所示:

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

这应该可以解决错误。

关于Java 编程 — 标记 "."出现语法错误,@ 在此标记之后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60395420/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com