This is supposed to be part of a simple interpreter with several keywords, which I made into different classes. The program is supposed to iterate over the ArrayList, tokenize the Strings and parse them into KEYWORD + instructions. I’m using a hashmap to map all these keywords to an interface which has classes, where the rest of the processing takes place. Currently testing out one of these keyword classes, but when I try to compile the compiler throws “identifier expected” and “illegal start of type” messages. The line throwing all the error messages is line 18. Where is the code going wonky? I can’t tell since I’ve never used a HashTable before. Thanks for the help!
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);
}
}
}
}
To get past your current compiler error, you’ll need to put the
DirectiveHash.put("END", new endStatement());call inside a block of some kind. If you want it in the instance initializer, try this:{
DirectiveHash.put(“END”, new endStatement());
}