问题
I can't work out what is wrong with the following code in Java:
import java.util.*;
public class stringCalculator {
String operator_array[] = {"+", "-", "/", "*", "(", ")"};
Queue<Integer> outputQueue = new LinkedList<Integer>();
Stack <Object> operatorStack = new Stack<Object>();
Hashtable<String, String> operatorPrecedence = new Hashtable<String, String>();
operatorPrecedence.put("+", "2");
I am getting the following error:
Syntax error on tokens, delete these tokens, This is in relation to the following line:
operatorPrecedence.put("+", "2");
Thanks for any help
回答1:
You can not put statements directly inside a class.
Create a method or a constructor and put the call to put
there.
public stringCalculator() {
operatorPrecedence.put("+", "2");
}
Also, it is good practice to use an upper-case name for the class, like StringCalculator
.
来源:https://stackoverflow.com/questions/20701477/unknown-error-relating-to-syntax-of-tokens