2016-03-08 106 views
0

我有一個文本文件解析,這需要根據特定條件的不同邏輯。下面是我當前的解決方案。但是,我發現它非常笨重,並且正在研究其他解決方案,如StringTokenizer或Pattern類,並且我想知道我可以使用它們更優雅地實現它。Java解析當前解決方案的替代

是否讓我知道我是否應該將其移至代碼評論論壇 - 我最初並沒有將它放在那裏,因爲我無法實現其他提到的解決方案。

File file = fileChooser.getSelectedFile(); 
    java.io.BufferedReader reader = new java.io.BufferedReader(new java.io.FileReader(file)); 

    memoryMap = new HashMap<Integer, Integer>(); 
    registerMap = new HashMap<Integer, Integer>(); 

    String line = reader.readLine(); 
    while (line != null) { 

     if (line.contains("#")) { 
      System.out.println(line); 
      line = reader.readLine(); 
     } 

     if (!Character.isDigit(line.charAt(0))) { 
      System.out.println(line); 
      String[] setFirstSplit = line.split(":"); 
      if (setFirstSplit[0].equals("M")) { 
       boolean isFirst = true; 
       for (String setFirstSegment : setFirstSplit) { 
        if (!isFirst) { 
         String[] setSecondSplit = setFirstSegment.split(","); 
         for (String setSecondSegment : setSecondSplit) { 
          String[] setThirdSplit = setSecondSegment.split("="); 
          for (String setThirdSegment : setThirdSplit) { 
           System.out.println(setThirdSegment); 
           memoryMap.put(Integer.parseInt(setThirdSplit[0]), Integer.parseInt(setThirdSplit[1])); 
           System.out.println("Memory Set Result: " + memoryMap); 
          } 
         } 
        } else { 
         isFirst = false; 
        } 
       } 
      } 
      if (setFirstSplit[0].equals("R")) { 
       boolean isFirst = true; 
       for (String setFirstSegment : setFirstSplit) { 
        if (!isFirst) { 
         String[] setSecondSplit = setFirstSegment.split(","); 
         for (String setSecondSegment : setSecondSplit) { 
          String[] setThirdSplit = setSecondSegment.split("="); 
          for (String setThirdSegment : setThirdSplit) { 
           System.out.println(setThirdSegment); 
           registerMap.put(Integer.parseInt(setThirdSplit[0]), Integer.parseInt(setThirdSplit[1])); 
           System.out.println("Register Set Result: " + registerMap); 
          } 
         } 
        } else { 
         isFirst = false; 
        } 
       } 
      } 
      line = reader.readLine(); 
     } else { 
      System.out.println(line); 
      String[] actionFirstSplit = line.split(" "); 
      if (actionFirstSplit[1].equals("LOAD")) { 
       String[] actionSecondSplit = actionFirstSplit[2].split(","); 
       LoadStep action = new LoadStep(); 
       action.executeStep(Integer.parseInt(actionSecondSplit[0]), Integer.parseInt(actionSecondSplit[1])); 
       System.out.println("Memory Action Result: " + memoryMap); 
       System.out.println("Register Action Result: " + registerMap); 
      } 
      else { 
       System.out.println(line); 
      } 
      line = reader.readLine(); 
     } 
    } 
    reader.close(); 

文本文件看起來像這樣:

# sets the memory address 0 to store the value 1. M stands for memory. 
M:0=1,1=11 
# All programs starts with an initial setup of values in memory such as the example shown above 
0 LOAD 1,3 
1 LOAD 0,2 
2 ADD 1,2 
3 ADD 0,1 
4 LSS 1,3,2 
5 STOR 62,1 
6 STOP 

回答

0

寫自上而下的。

String line = reader.readLine(); 
while (line != null) { 
    if (parsedComment(line)) { 
    } else if (parsedMemory(line)) { 
    } else if (parsedInstruction(line)) { 
    } else { 
     error(...); 
    } 
    line = reader.readLine(); 
} 

解析函數可以使用字段來傳遞結果,像那些地圖,或具有額外的參數。

(如果您具有多行語法,讀者可能會更好地放置在一個領域,消失參數。然後,您可以在現場讀取一行進取,檢查這一點。)