-2

我想计算程序中的代码行数,并且我已经为此编写了计算;. 它可以正常工作,但在某些情况下(例如没有;存在的情况下(例如ifwhile语句))就不行。在这种情况下,我将一些关键字存储在一个字符串数组中,我想使用readLine(). 如果它工作正常,那么我会将它增加 1,但它不起作用。我已经尝试了很多,但它根本不起作用,并且显示异常。因为Demo.java您可以使用自己的代码。

类检测1.java

import java.io.*;
public class Classdetect1
{
    public static void main(String[] args) throws IOException
    {
        int i=0,j=0,k=0,p;
        String str1[]={"if","else","else if","while","for","goto","do"};

        // Open the file
        FileInputStream fstream = new FileInputStream("Demo.java");



        BufferedReader br = new BufferedReader(new InputStreamReader(in));

        String strLine;

        if (in == null){
            System.out.println("File is blank");
            System.exit(0);
        }

        while (i != -1)
        {
            i = in.read();
            if(i==';' || i=='{')
            {
                j=j+1;
            }

            if(i=='\'')
            {
                while(in.read()!='\'')
                    continue;
            }

            if(i=='\"')
            {
                while(in.read()!='\"')
                    continue;
            }

            if(i=='(')
            {
                while(in.read()!=')')
                    continue;
            }


            while ((strLine = br.readLine()) != null)
            {
                for(p=0;p<7;p++)
                {
                    if(str[p].equals(strLine))
                    {
                        k=k+1;
                    }
                }
            }
            System.out.println("Line of =" + k);
        }
        System.out.println("Total Line of code=" + j);
    }
}
4

2 回答 2

0

在 Java 中,所有语句都;{ }. 一些例子:

System.out.println("One statement");

while (str.equals("One block of statements + one statement"))
{
    System.out.println("One statement");
}

此外,;根本不需要在同一行:

System.out.println(
    "One statement"
);

所以你可以简单地计算所有;(语句结束)和所有{(语句结束,块开始),它会相当准确。

if (true)
{                               // One statement ending with '{'
    doSomething();              // One statement ending with ';'
    while (false)
    {                           // One statement ending with '{'
        doSomethingElse();      // One statement ending with ';'
    }
}
// 4 statements in total.

当然,(一如既往)有一些例外:

if (true) doSomething();  // One statement, or two?

do { doSomething(); } while (true); // Three statements, or two?
于 2013-04-21T12:29:23.537 回答
0

尝试这个:

BufferedReader br = new BufferedReader(new FileReader(new File("C:/lines.txt")));
String s = "";
String text = "";

//save all the file in a string
while ((s = br.readLine()) != null) {
    text += s + "\n";
}

//remove empty lines with a regex
String withoutEmptyLines = text.replaceAll("(?m)^[ \t]*\r?\n", "");

//lines of code = text with newline characters length - text without newline characters length
int linesOfCode = withoutEmptyLines.length() - withoutEmptyLines.replaceAll("\n", "").length();
System.out.println("Lines: "+linesOfCode);

我的C:/lines.txt文件:

01. a
02. 
03. b
04. c
05. c
06. d
07. as
08. d
09. asd
10. asd
11. a
12. 
13. 
14. 
15. asd
16. 
17. asd
18. 
19. 
20. 
21. asdasd
22. 
23. 
24. 

使用此文件,输出为:

Lines: 13

希望这可以帮助

于 2013-04-21T12:30:21.207 回答