我正在进行Java项目。其中有一个部分,我需要识别程序中的单行注释、多行注释和总注释数量。我需要您的指导来计算Java中的单行注释、多行注释和总注释行数。
我正在进行Java项目。其中有一个部分,我需要识别程序中的单行注释、多行注释和总注释数量。我需要您的指导来计算Java中的单行注释、多行注释和总注释行数。
如果你只想要有关注释和代码行数的摘要,可以查看CLOC。将其指向源目录,例如:
cloc src
...它将为您显示摘要。
CLOC还处理使自己解决此问题变得困难的边角情况-多行注释、字符串中的注释查找行等。以下内容:
package test;
public class Main {
/**
* A javadoc comment.
* @param args
*/
public static void main(String[] args) {
System.out.println("/* This isn't a comment */");
/* This is a comment */
//Comment
System.out.println("//Not comment");
}
//Comment
}
CLOC给出了2个空行、7个注释行和7个代码行。
是的,你可以自己用Java编写一些东西,但除非这是一个特定的作业问题(在这种情况下应该标记),否则为什么要重新发明轮子呢?你需要处理大量边角情况,进行大量测试以确保它工作正常,将其与现有工具进行比较等等,而当你已经有一个很好地完成工作的东西时,这样做真的没有意义。
/
)。如果我们有一些带有这种转义序列的代码,那么我们可以在首先转换为UTF-8后使用native2ascii(在“-reverse”模式下)来删除它们。 - Paŭlo Ebermann这是我针对同样问题做的一个作业的链接,希望能对你有所帮助。
注意:它不完整,因为它没有计算Java中字符串(双引号内)中包含转义字符的双引号(如“/\”/”)作为代码行,而是将其视为注释。这个问题仍需要解决。
以下是解释:
Note :- This program is not optimized for performance, performance is not an attribute i care about here. I chose not to use Pattern.compile() among many other decisions.
I count the empty lines, comment lines and lines of code separately.
if there is any sentence which has a some tabs or just spaces, they are counted as
empty lines. They match with the regular expression \s*
\s - any whitespace character (\n\t\b)
* - this means there can be any number of whitespace characters
comment lines are the ones that match any of the following conditions :-
1) have a // outside of a string
2) have a /* some comment */
3) have a /* but the '*/' is not found on the same line
The regular expression for the above can be found inside the code!
Lines of code are the ones that end in ')' , '}' , '{' or ';' (one problem with this approach is that it does not count lines that have code followed by comment as a line of code)
The summary of these three types of lines are provided by this program.
它适用于我。它计算了Java文件中单行和多行注释的总数。
public class CountComment {
public static void main(String[] args) throws IOException {
try {
String str;
BufferedReader f = new BufferedReader(new FileReader("a.java"));
LineNumberReader l = new LineNumberReader(f);
int numberline = 0;
int count = 0;
while ((str = l.readLine()) != null) {
numberline++;
if (str.startsWith("/*")) {
while ((str = l.readLine()) != null) {
count++;
if (str.endsWith("*/")) {
count++;
break;
}
}
}
else if(str.startsWith("//")){
count++;
}
}
System.out.println("" + count);
} catch (FileNotFoundException e) {
System.out.println("file not found");
} catch (IOException e) {
e.printStackTrace();
}
}
}
我的答案是:
public int countCommentLines(File file) throws IOException {
BufferedReader input = new BufferedReader(new FileReader(file));
int comments = 0;
String line;
while ((line = input.readLine()) != null) {
line = line.trim();
System.out.println(line);
if (line.startsWith("//")) {
//single line
comments++;
} else if (line.startsWith("/*")) {
/* multiline
* like this
* */
comments++;
while (!line.endsWith("*/")) {
line = input.readLine().trim();
comments++;
}
} else if (line.contains("/*")) { /* comments
like this
be careful with /* haha
*/
Deque<Character> stack = new ArrayDeque<Character>();
boolean hasStartOfComment = false;
for (int i = 0; i < line.length() - 1; ++i) {
if (stack.isEmpty() && line.charAt(i) == '/' && line.charAt(i + 1) == '*') {
// Yes this is the start of a block comment
hasStartOfComment = true;
break;
} else if (line.charAt(i) == '\\'
&& line.charAt(i + 1) == '\"' || line.charAt(i + 1) == '\'') {
// Well, it's escaped character
i++;
continue;
} else if (line.charAt(i) == '\'' || line.charAt(i) == '\"') {
if (!stack.isEmpty() && stack.peek() == line.charAt(i)) {
stack.pop();
} else {
stack.push(line.charAt(i));
}
continue;
}
}
if (!hasStartOfComment) {
continue;
}
while (!line.endsWith("*/")) {
line = input.readLine().trim();
comments++;
}
}
}
return comments;
}
在Java中,您有两种类型的注释:
// - 行注释
/* */ - 块注释。
基于此,您可以做出简单的假设:
如果行中存在“//”,则该行至少有一个注释。
如果行中存在“/*”,则已打开注释块。
如果注释块已打开(找到),请搜索关闭标记“*/”。
因此,您的任务是检查文件中的每一行是否符合这些假设。
String s ="//";
没有注释,2)行// single /* line
不会开始块注释,3)序列*\u002f
结束块注释。 - Christian Semrau在我的一个课程作业中,我编写了一个简单的Java代码来完成这个任务。 https://github.com/chetanpawar0989/CommentAnalysis
我之前创建了这个文件,但最近添加到了Github上。 该程序逐行运行一个文件。它在运行时需要一个输入参数filename.java
例如,要运行此程序:java CommentAnalysis filename.java
它会计算单行注释(//)以及多行注释(/* ... */)。
我尝试涵盖this file中提到的不同注释场景。
我还在自述文件中写下了我的实现假设,但根据个人需求进行更改应该不难。请查看并告诉我是否遗漏了任何注释场景。
//Below is the working code
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class CountComment {
public static void main(String[] args) {
String line="";
int count=0;
try {
BufferedReader br=new BufferedReader(new FileReader("./src/numbers/comment.txt"));
while((line=br.readLine())!=null)
{
if(line.startsWith("//"))
count++;
if(line.startsWith("/*"))
{
count++;
while(!(line=br.readLine()).endsWith("'*\'"))
{
count++;
break;
}
}
}
br.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
System.out.println("count="+count);
}
}
我尝试过它,它运行得非常好。使用下面的代码,您将需要检查每一行:
if (isEmpty(s) || s.IndexOf("\"//\"") != -1 || s.IndexOf("\"/*\"") != -1|| s.IndexOf("\"*/\"") != -1) continue;
if (s.IndexOf("/*") != -1) checkCML = true;
if (s.IndexOf("*/") != -1) checkCML = false;
if (s.IndexOf("/*") != -1 && s.IndexOf("*/") != -1) ++commentLine;
if (s.IndexOf("//") != -1 || checkCML) ++commentLine;