Unknown error relating to syntax of tokens

风格不统一 提交于 2019-12-02 09:19:30

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!