2013-03-12 65 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