剥离评论和评论的项目?

时间:2015-01-17 02:11:51

标签: java comments java.util.scanner

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
    }  
}

在评论的所有内容消失后应该是什么样子:

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!");
    }
}

有什么想法吗?如果以某种方式我可以在评论符号等之后删除文本。

0 个答案:

没有答案
相关问题