2012-01-12 49 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

相關問題