令牌上的语法错误 [英] Syntax error on tokens
本文介绍了令牌上的语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我收到一个我不明白的错误:
此行上的多个标记
- 令牌上的语法错误,错放
construct(s)
- 令牌上的语法错误,删除这些令牌
以下是我的类代码,错误发生在第8行(标记):
import java.util。*;
pre>
public class stringCalculator {
String operator_array [] = {+, - ,/,*,(,)};
队列<整数> outputQueue = new LinkedList< Integer>();
堆栈< Object> operatorStack = new Stack< Object>();
Hashtable< String,Integer> precendece = new Hashtable< String,Integer>();
precedence.put(+,2); < ===========这是发生错误的地方
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); (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(/)| |表达式[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)
{
尝试
{
double number = Double.parseDouble(expression);
}
catch(NumberFormatException nfe)
{
return false;
}
返回true;
}
public void checkPrecedence()
{
}
}
解决方案语句
precedence.put(+,2);
必须在方法或块内。
例如,您可以将其放在构造函数中
public stringCalculator(){
precedence.put(+,2);
}
与您所遇到的问题无关,课程需要以大写字母开头,根据 Java命名约定
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):
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() { } }
解决方案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); }
Not related to the problem you have, classes need to start with a capital letter, according to the Java Naming Conventions
这篇关于令牌上的语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文