令牌的语法错误 [英] Syntax error on tokens

查看:30
本文介绍了令牌的语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到一个我不明白的错误:

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

以下是我的类代码,第8行出现错误(标记):

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()
    {

    }
}

推荐答案

语句 precedence.put("+", 2); 必须在方法或块中.

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);
}

与您遇到的问题无关,类需要以大写字母开头,根据Java 命名约定

Not related to the problem you have, classes need to start with a capital letter, according to the Java Naming Conventions

这篇关于令牌的语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆