Java 是否剥离注释和已注释的项目?

Java 是否剥离注释和已注释的项目?,java,comments,java.util.scanner,Java,Comments,Java.util.scanner,代码到目前为止。它将删除注释和符号。但它不会删除任何被注释掉的内容或注释之间的内容。例如: public static void stripComments(Scanner input) { while (input.hasNextLine()) { String noComments = input.nextLine().replace("//", "").replace("*/", "").replace("/*", ""); Syst

代码到目前为止。它将删除注释和符号。但它不会删除任何被注释掉的内容或注释之间的内容。例如:

public static void stripComments(Scanner input) {
    while (input.hasNextLine()) {        
        String noComments = input.nextLine().replace("//", "").replace("*/", "").replace("/*", "");
        System.out.println(noComments);      
    }
}
在所有被评论的内容都消失后,它应该是什么样子:

import java.util.*;
 My program - NEEDS TO BE GONE
by Suzy Student - NEEDS TO BE GON
public class Program {
    public static void main(String[] args) {
        System.out.println("Hello, world!");  a println - NEEDS TO BE GONE
    }

    public static  (Hello there - NEEDS TO BE GONE)  void foo() {
        System.out.println("Goodbye!");  comment here - NEEDS TO BE GONE
    }  
}

有什么想法吗?如果我能以某种方式删除注释符号后的文本等。

为什么要这样做?您只需替换
/
——您需要替换整行(或
/*
*/
中的文本,如果适用)。查看或谷歌搜索其他信息。请注意,您可以使用正则表达式来标识换行符、回车符等。然后我将尝试使用正则表达式。这样做,因为这是我的Java书籍和类的一个练习。
import java.util.*;

public class Program {
    public static void main(String[] args) {
        System.out.println("Hello, world!");
    }

    public static  void foo() {
        System.out.println("Goodbye!");
    }
}