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
这篇关于令牌的语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:令牌的语法错误


基础教程推荐
- 不推荐使用 Api 注释的描述 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- 大摇大摆的枚举 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 从 python 访问 JVM 2022-01-01
- 多个组件的复杂布局 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01