【发布时间】:2011-09-17 20:57:20
【问题描述】:
我必须删除文件中的所有 cmets。引号内的注释分隔符应被视为文本,并且必须打印到屏幕上。 cmets 中的引号被视为其他 cmets,必须删除。
当涉及到单引号时,下面的 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);
}
}
}
【问题讨论】:
-
我可能是错的,但这似乎是 Regular Expressions 的工作
标签: java quotes switch-statement