三种comments:

 /* Test program */

    int main()

    {

       // variable declaration

       int a, b, c;

       /* This is a test

           multiline

           comment for

           testing */

       a = b + c;

    } 
 import java.util.*;
public class removeComments{
public static void main(String [] args){
String file = " /* Test program */ \n" +
" int main() \n" +
" { \n" +
" // variable declaration \n" +
" int a, b, c; \n" +
" /* This is a test \n" +
" multiline \n" +
" comment for \n" +
" testing */ \n" +
" a = b + c; \n" +
" } \n";
System.out.println(file);
System.out.println(removeComments(file));
} private static String removeComments(String s){
if(s == null || s.length() == 0){
return s;
}
StringBuilder res = new StringBuilder();
boolean sCom = false;
boolean mCom = false; for(int i = 0; i<s.length(); i++){
// System.out.println("i is " + i + "sCom, mCom = " + sCom + mCom);
if(sCom && (s.charAt(i) == '\n')){
System.out.println("i is " + i);
sCom = false;
}else if(mCom && s.charAt(i) == '*' && i+1<s.length() && s.charAt(i+1) == '/'){
mCom = false;
i++;
}else if(sCom || mCom){
continue;
}else if(s.charAt(i) == '/' && i+1 < s.length() && s.charAt(i+1) == '/'){
sCom = true;
i++;
}else if(s.charAt(i) == '/' && i+1 <s.length() && s.charAt(i+1) == '*'){
mCom = true;
i++;
}else{
res.append(s.charAt(i));
}
}
return res.toString();
} }
05-11 15:20