Syntax error on tokens(令牌的语法错误)
问题描述
我收到一个我不明白的错误:
I am getting an error which i don't understand:
此行有多个标记- 令牌上的语法错误,放错了位置结构体)- 标记的语法错误,删除这些标记
Multiple markers at this line - Syntax error on token(s), misplaced construct(s) - Syntax error on tokens, delete these tokens
以下是我的类代码,第8行出现错误(标记):
The following is my code for the class, error occurs in line 8 (marked):
import java.util.*;
public class stringCalculator {
String operator_array[] = {"+", "-", "/", "*", "(", ")"};
Queue<Integer> outputQueue = new LinkedList<Integer>();
Stack <Object> operatorStack = new Stack<Object>();
Hashtable<String, Integer> precendece = new Hashtable<String, Integer>();
precedence.put("+", 2); <=========== This is where the error occurs
public void printTokenList(String [] expression, int length)
{
for(int i = 0; i < length; i++){
System.out.println(expression[i]);
}
}
public void checkInput(String [] expression, int length)
{
System.out.println(expression);
for(int i = 0; i < length; i ++){
if(checkIfNumber(expression[i])){
int new_expression = Integer.parseInt(expression[i]);
outputQueue.add(new_expression);
}
else if(expression[i].equals("+") || expression[i].equals("-") || expression[i].equals("/") || expression[i].equals("*")){
for(int j = 0; j < 6; j++){
if(expression[i].equals(operator_array[j])){
operatorStack.push(expression[i]);
}
}
}
}
}
public static boolean checkIfNumber(String expression)
{
try
{
double number = Double.parseDouble(expression);
}
catch(NumberFormatException nfe)
{
return false;
}
return true;
}
public void checkPrecedence()
{
}
}
推荐答案
语句 precedence.put("+", 2);
必须在方法或块中.
The statement precedence.put("+", 2);
has to be within a method or a block.
例如,你可以把它放在构造函数中
For example, you can place it within the constructor
public stringCalculator() {
precedence.put("+", 2);
}
与您遇到的问题无关,类需要以大写字母开头,根据Java 命名约定
Not related to the problem you have, classes need to start with a capital letter, according to the Java Naming Conventions
这篇关于令牌的语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:令牌的语法错误


- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01