参见英文答案 > Unknown error relating to syntax of tokens 1个
我收到一个我不明白的错误:
此行有多个标记
– 令牌上的语法错误,放错位置
结构体)
– 令牌上的语法错误,删除这些令牌
以下是我的类代码,错误发生在第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()
{
}
}
解决方法:
语句precedence.put(“”,2);必须在方法或块内.
例如,您可以将其放在构造函数中
public stringCalculator() {
precedence.put("+", 2);
}
根据Java Naming Conventions,与你所遇到的问题无关,类需要以大写字母开头