2013-03-12 62 views
3

所以我有這個文本文件驗證一個LaTeX文件,JAVA

\begin{document} 
    {\Large \begin{center} Homework Problems \end{center}}\begin{itemize}\item\end{itemize} 
    \begin{enumerate} 
        \begin{proof} 
          \begin{align} 

          \end{align} 
        \end{proof} 

        \begin{proof} 

          \begin{align} 

          \end{align} 

        \end{proof} 
    \end{enumerate} 
\end{document} 

而且我想通過每一行,找到所有的「\開始」片,然後把字符串中的「{ _}「並將其存儲在堆棧中。當找到相應的「\ end」時,我在堆棧上調用pop()命令並將其刪除。我雖然有幾個問題...

  1. 我正在與各種瘋狂的情況下處理,並確保一切都被容納其變得過於特殊,這種情況下,當我想使它工作對於像這樣寫的各種文件。
  2. 我不知道如何檢查「\ begin」和「\ end」而不是「begin」和「end」,這個很重要的原因是因爲如果文件包含說「開始」的文本或「結束」它可能不是一個命令,因此,不是我正在尋找的。

所有「if」語句都不適用於存在「\」的情況,我嘗試添加方括號,但它沒有解決任何問題。

這是我的代碼到目前爲止,它變得非常混亂,任何人都可以幫助組織和幫助糾正我上面提到的問題嗎?

import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.util.Scanner; 
import java.util.Stack; 
import java.util.StringTokenizer; 

public class LaTeXParser{ 

public static void main(String args[]) throws FileNotFoundException{ 

    Scanner scan = new Scanner(System.in); 

    Stack s = new Stack(); 

    int lineCount = 0; 

    String line; 
    String nextData = null; 
    String title = null; 

      String fname; 

      System.out.print("Enter the name of the file (no extension): "); 
      fname = scan.next(); 

      fname = fname + ".txt"; 

      FileInputStream fstream = new FileInputStream(fname); 

      Scanner fscan = new Scanner(fstream); 

      System.out.println(); 

      while(fscan.hasNextLine()){ 

       lineCount++; 
       line = fscan.nextLine(); 
       StringTokenizer tok = new StringTokenizer(line); 

       while(tok.hasMoreElements()){ 

        nextData = tok.nextToken(); 
        System.out.println("The line: "+nextData); 

        if(nextData.contains("\\begin") && !nextData.contains("\\end")){ 

         if(nextData.charAt(1) == 'b'){ 

          title = nextData.substring(nextData.indexOf("{") + 1, nextData.indexOf("}")); 

          s.push(title); 

         } 

         else{ 

          //title = nextData.substring(); 

         } 
        }//end of BEGIN if 

        if(nextData.contains("\\end") && !nextData.contains("\\begin")){ 

         if(s.peek().equals(nextData.substring(nextData.indexOf("{") + 1, nextData.indexOf("}")))){ 

          s.pop(); 

         } 
        }//end of END if 

        if(nextData.contains("\\begin") && nextData.contains("\\end")){ 

         String[] theLine = nextData.split("[{}]"); 

         for(int i = 0 ; i < theLine.length ; i++){ 

          if(theLine[i].equals("\\end") && theLine[i+1].equals(s.peek())){ 

           s.pop(); 

          } 

          if(theLine[i].equals("\\begin")){ 

           title = theLine[i+1]; 

           s.push(title); 

          } 


         } 

        }//end of BEGIN AND END if 

       } 
      }//end of whiles 

      fscan.close(); 

    while(!s.isEmpty()){ 

     System.out.println("the top "+s.pop()); 

    } 
} 
} 

編輯:用來檢查線路,看它是否包含發現後既「\開始」和「\結束」 if語句的「\開始」,我怎麼回去過檢查該行是否也包含「\ end」?所以我講的情況......

\begin{itemize}\item\end{itemize} 

看,我能到「\開始」,並添加適當的字符串,但它只是移動並通過了「\ {結束}逐項」。有任何解決這個問題的方法嗎?

事實上,即使在「itemize」字符串被推入後,它也應該正常檢查並執行,但它不起作用!我相信它與「\ end」有關,任何人都可以確認嗎?它跳過這一步,顯然是因爲它不符合條件,但它適用於其他線路。只是不是這個具體案例!

+0

您是否使用調試器一步一步進入您的代碼? – 2013-03-12 06:24:20

+0

不是,我一直在使用調試方法來測試它,printlns等。 – Sherifftwinkie 2013-03-12 06:26:15

回答

1

您可能需要避開反斜槓,因此請寫\\而不是\。如果它們是正則表達式(正則表達式),則需要將它們轉義兩次:\\\\;我不認爲括號是必要的。

+0

你是什麼意思的「正則表達式」?而且我怎麼知道什麼時候有什麼東西在「入」呢? – Sherifftwinkie 2013-03-12 06:21:01