2011-06-18 43 views
-1

我必須刪除文件中的所有註釋。引號內的註釋分隔符應視爲文本,並且必須打印到屏幕上。評論內的評論被視爲其他評論,必須刪除。在switch語句中表示單引號的錯誤

我在遇到下面的switch語句時遇到了單引號問題,它給了我一個錯誤。

我知道錯誤是什麼,但我只是不知道如何解決它。

import java.io.*; 

public class Q3{ 

public static final int STATE_NORMAL = 0; 
public static final int STATE_QUOTE_SINGLE = 1; // this:'' 
public static final int STATE_QUOTE_DOUBLE = 5; // this: "" 
public static final int STATE_SLASH = 2;   // this:/
public static final int STATE_SLASH_STAR = 3;  // this: /* * 
public static final int STATE_COMMENT = 4;   // this: /*  */ || /* only 

public static void main(String[] argv) throws IOException{ 
    final int EOF = -1; 
    InputStreamReader in = new InputStreamReader(System.in); 
    int c; 
    char[] buffer = new char[2]; 
    int currState = Q3.STATE_NORMAL; 
    char outChar;   
    while((c = in.read()) != EOF){ 

     outChar = (char) c; 
     switch (outChar){ 

      case '/' : 
      /* if(currState == Q3.STATE_QUOTES) 
        currState ==Q3.STATE_QUOTES;   */ 
       if(currState == Q3.STATE_NORMAL) 
        currState = Q3.STATE_SLASH; 
       else if(currState == Q3.STATE_COMMENT) 
        currState = Q3.STATE_NORMAL; 


       break; 

      case '*': 
       if(currState == Q3.STATE_SLASH) 
        currState = Q3.STATE_COMMENT; 

       break; 

      case '"': 
       if(currState == Q3.STATE_NORMAL) 
        currState = Q3.STATE_QUOTE_DOUBLE; 
       if(currState == Q3.STATE_QUOTE_DOUBLE) 
        currState = Q3.STATE_NORMAL; 

       break; 

      *case ''': 
       if(currState == Q3.STATE_NORMAL) 
        currState = Q3.STATE_QUOTE_SINGLE; 
       if(currState == Q3.STATE_QUOTE_SINGLE) 
        currState = Q3.STATE_NORMAL; 

       break;* 


     }  
     if(currState != Q3.STATE_COMMENT)    
      System.out.print(outChar); 
     } 
    } 
    } 
+0

我可能是錯的,但這看起來像[正則表達式]的工作(http://download.oracle.com/javase/tutorial/essential/regex/) – mre

回答

4

您將不得不逃避單引號。 使用本

case '\'' 
+0

perfecttoooooo !!! – Nidale

0

嘗試以下方法: STR = str.replaceAll( 「/ + /」, 「」);我不確定上下文,但是想法是使用正則表達式而不是像你正在嘗試的簡化代碼。

+0

我們應該使用狀態「只是作爲dfa或nfa」 – Nidale