2013-04-21 29 views
-2

我想計算一個程序內的代碼行數,並且我已經編寫了代碼來計算;的數量。它工作正常,但在某些情況下不存在;(如ifwhile語句)。在這種情況下,我將一些關鍵字存儲在一個字符串數組中,我想使用readLine()來搜索該關鍵字。如果它工作正常,那麼我會增加1,但它不起作用。我嘗試了很多,但它根本不工作,它顯示了異常。由於Demo.java您可以使用自己的代碼。如何計算程序中的代碼行數?

Classdetect1.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); 
    } 
} 
+2

什麼是_The EXCEPTION_,以及在哪裏呢_it_發生的呢? – Virtlink 2013-04-21 12:17:58

+0

考慮寫一個解析器,讀一下Antlr – 2013-04-21 12:21:01

+1

'str [p] .equals(strLine)',你在哪裏定義了名爲'str'的​​數組? – NINCOMPOOP 2013-04-21 12:22:41

回答

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

希望這有助於

相關問題