Java哈希表错误 - 标识符预期和非法类型的启动?

Lui*_*hil 0 java hashtable

这应该是具有多个关键字的简单解释器的一部分,我将其制作成不同的类.该程序应该遍历ArrayList,将字符串标记化并将它们解析为KEYWORD +指令.我正在使用散列映射将所有这些关键字映射到具有类的接口,其中进行其余的处理.目前正在测试其中一个关键字类,但是当我尝试编译时,编译器会抛出"标识符预期"和"非法启动类型"消息.抛出所有错误消息的行是第18行.代码在哪里变得难以理解?我无法分辨,因为我之前从未使用过HashTable.谢谢您的帮助!

import java.util.*;

public class StringSplit
{
interface Directive //Map keywords to an interface
{
    public void execute (String line);
}
    abstract class endStatement implements Directive
    {
        public void execute(String line, HashMap DirectiveHash)
        {   
            System.out.print("TPL finished OK [" + " x lines processed]");
            System.exit(0);
        }
    }
    private Map<String, Directive> DirectiveHash= new HashMap<String, Directive>();
    DirectiveHash.put("END", new endStatement());

    public static void main (String[]args)
    {
        List <String> myString= new ArrayList<String>();
        myString.add(new String("# A TPL HELLO WORLD PROGRAM"));
        myString.add(new String("STRING myString"));
        myString.add(new String("INTEGER myInt"));
        myString.add(new String("LET myString= \"HELLO WORLD\""));
        myString.add(new String("PRINTLN myString"));
        myString.add(new String("PRINTLN HELLO WORLD"));
        myString.add(new String("END"));


        for (String listString: myString)//iterate across arraylist
        {   
                String[] line = listString.split("[\\s+]",2);
                for(int i=0; i<line.length; i++)
                {
                    System.out.println(line[i]);
                    Directive DirectiveHash=DirectiveHash.get(listString[0]);
                    DirectiveHash.execute(listString);

                }

        }

    }
}
Run Code Online (Sandbox Code Playgroud)

jbi*_*del 8

要超越当前的编译器错误,您需要将DirectiveHash.put("END", new endStatement());调用放在某种块中.如果您想在实例初始化程序中使用它,请尝试以下操作:

{DirectiveHash.put("END",new endStatement()); }

  • 你有另一个错误,即`endStatement`类没有实现`public void execute(String line);`因为你用一个额外的参数来定义它.你也无法使用`new endStatement()`实例化`abstract`类,因为它是*abstract*. (3认同)