2012-01-12 52 views
0

我正在尝试构建一个简单的解释器。基本上我使用这种方法从的字符串中获取我的HashMap的密钥。有8种不同的可能性(8个关键字),HashMap中的一个字符串可以开头。目前,我正在使用string.indexOf("something")来查找关键字字符串,但是当我拥有多个关键字时,这当然并不灵活。使用Java string.indexOf()返回不同的子字符串 - 可能吗?

所有在ArrayList的字符串可以被分解成COMMAND +(指令)。 COMMANDS映射到HashMap及其类。所以基本上这是一个2步骤的情况:第一次通过我需要从字符串中获得第一个单词/标记,然后字符串的其余部分最好在相应的类中进一步分割/标记。

反正是有string.indexOf()可以在某种程度上操纵返回多个子串的指数?或者我需要寻找其他方法吗?请指教。

代码如下所示:

public void parseCommands() { 
    List<String> myString = new ArrayList<String>(); 
    myString.add(new String("# A TPL HELLO WORLD PROGRAM")); 
    myString.add(new String("# xxx")); 
    myString.add(new String("STRING myString")); 
    //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")); 

    System.out.println(); 
    for (String listString : myString)//iterate across arraylist 
    { 
     if (listString.startsWith("#", 0))//ignore comments starting with # 
     { 
      continue; 
     } 

     int firstToken = listString.indexOf("END"); 
     String command = listString; 


     Directive directive = commandHash.get(command); 
     if (directive != null) { 
      directive.execute(listString); 
     } else { 
      System.out.println("No mapped command given"); 
     } 
    } 
} 
+0

'indexOf'不能被操纵返回比它已经返回的任何其他。不知道你的输入字符串是什么样的,不可能提供帮助 - 一个简单的split()就足够了吗? – 2012-01-12 19:55:25

+0

您可能想了解Java的RegEx功能......需要一些阅读,尝试和错误以及努力,但肯定会完成这项工作。 – Fildor 2012-01-12 19:56:22

+1

你似乎没有使用'firstToken'。你的解释不够清楚,以反映要求。 – 2012-01-12 20:02:59

回答

0

简短的回答是否定的。您可能想要使用String.split(),StreamTokenizer或StringTokenizer。

2

看起来像在AL每个串既可以只是一个用于命令的命令或命令和输入。

我认为你可以在这里使用split方法:

String[] parts = listString.split(" "); 

如果parts的大小是一个,这意味着它只是一个命令,否则parts[0]是一个命令,剩下的,该指令输入。

执行查找它:

Directive directive = commandHash.get(parts[0]); 

然后如果返回Directive然后

  1. 如果parts的长度为1,那么就去做directive.execute()
  2. 否则,形成与parts其余部分的输入和做directive.execute(input)

如果情况并非如此,我可能没有得到你想说的话。

而且,看到String,它拥有所有的排序,你可以利用此方法。

更新:

public interface Directive {  
    void execute(String input); 
} 

public class EndDirective implements Directive { 
    @Override 
    public void execute(String input) { 
     // input will be neglected here 
     // just do whatever you supposed to do 
    }  
} 

pubic class PrintlnDirective implements Directive { 
    @Override 
    public void execute(String input) { 
     // input will be used here   
     // you might want to check if the input is null here 
     // and write the code accordingly 
     System.out.println(input); 
    }  
} 

有了,你可以做directive.execute(null);,当你没有任何输入,因为你各Directive要么忽略输入或使用它(如果他们接受也可以处理空当他们期待一些输入时为空)。

+0

@βнɛƨнǤʋяʋиɢ谢谢,你说得对。 AL中的字符串全部可分解为命令+输入或命令(仅在END语句的情况下)。如果我使用'split()'方法得到一个数组,其中0和1有2个部分('parts [0]','parts [1]') - 我的接口指令只有这个方法public void execute String listString)';我需要在那里为'directive.execute(input)'添加另一个方法吗?谢谢! – Luinithil 2012-01-12 20:50:47

+0

是的add(重载)'directive.execute()',它不带任何参数,你会没事的。或者你也可以改变现有的一个来处理空输入。 – 2012-01-12 20:56:57

+0

@βнɛƨнǤʋяʋиɢ如果我想改变现有的一个来处理空输入,我需要在该方法中修改什么?对不起,如果这是一个noob问题,但我只是在认真编程和学习Java不到两个月 - 很多东西对我来说都是新的。 – Luinithil 2012-01-12 21:15:03

相关问题