Java 令牌上的语法错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20702050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Syntax error on tokens
提问by user3120023
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
此行有多个标记 - 标记上的语法错误,错位的构造 - 标记上的语法错误,删除这些标记
The following is my code for the class, error occurs in line 8 (marked):
以下是我的类代码,错误出现在第 8 行(已标记):
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()
{
}
}
回答by Konstantin Yovkov
The statement precedence.put("+", 2);
has to be within a method or a block.
该语句precedence.put("+", 2);
必须在方法或块中。
For example, you can place it within the constructor
例如,您可以将它放在构造函数中
public stringCalculator() {
precedence.put("+", 2);
}
Not related to the problem you have, classes need to start with a capital letter, according to the Java Naming Conventions
与您遇到的问题无关,根据Java 命名约定,类需要以大写字母开头
回答by Suresh Atta
Statements should be placed in inside constructors/methods/blocks, otherwise Compile time error occurs.
语句应该放在constructors/methods/blocks里面,否则会出现编译时错误。
precedence.put("+", 2); <=========== This is where the error occurs
Move that line to constructor/method.
将该行移动到构造函数/方法。
回答by gaurav5430
precedence.put("+", 2); <=========== This is where the error occurs
this statement is not inside any block , hence it is not permitted.
此语句不在任何块内,因此是不允许的。
remove it from here and put it inside any other method or block
从这里删除它并将其放入任何其他方法或块中
Note: this is how java works.
注意:这是java的工作方式。
for an extended discussion, please refer to:
有关扩展讨论,请参阅:
回答by MS-
precedence.put("+", 2);
The above line is incorrectly placed. You should initialise the Hashtable in constructor.
上面的行放置不正确。您应该在构造函数中初始化 Hashtable。