2014-12-15 1 views
-2

Am 텍스트 파일을 읽고 여러 줄 "/ * * /"주석 및 한 줄 주석 "//"을 제외하고 줄 수를 계산하려고합니다. 텍스트 파일에서 "myMethod() {}" 의 모든 메서드를 찾고 계산하여 텍스트 파일에있는 메서드의 총 수를 계산합니다. 텍스트 파일 "java MyCountLine C : \ myFolder \ myText.txt"와 같은 java 프로그램을 실행하는 동안 텍스트 파일이 추가됩니다. 텍스트 파일에서 메서드를 가져 오는 방법을 알아낼 수 없습니다.자바에서 주석 및 새 라인을 제외하고 텍스트 파일의 라인 수를 계산하십시오.

import java.io.*; 

public class LineCountss { 

public static void main(String[] args) { 

    File inFile = null; 

    if (0 < args.length) { 
     // text file will be passed during run time 
     inFile = new File(args[0]); 
    } else { 
     System.out.println("Cant Find The File Specified : " + inFile); 
    } 

    BufferedReader br = null; 

    String sCurrentLine = null; 

    String func = null; 

    int a = 0, b = 0, c = 0, k = 0; 

    try { 

     // passing the text file location for FileReader. 

     br = new BufferedReader(new FileReader(inFile)); 

     // Looping through the text file 

     while ((sCurrentLine = br.readLine()) != null) { 

      // avoid multi-line comments and one line comments and new 
      // lines. 

      if ((sCurrentLine.startsWith("/*") && sCurrentLine 
        .endsWith("*/")) 
        || sCurrentLine.startsWith("//") 
        || sCurrentLine.isEmpty() 
        || (sCurrentLine.trim().matches("[{};]+"))) { 

       // count the number of comment lines and new lines to 
       // exclude it from count. 

       b++; 

       // Getting any function in the text file that start and end 
       // with(). 

      } else if (sCurrentLine.contains("\\(\\)\\{")) { 

       func = sCurrentLine.trim(); 

       // printing the functions/methods 

       System.out.println(func); 

       // counting the number of functions/ methods found 

       k++; 

      } else { 

       // printing the text file just for checking 

       System.out.println(sCurrentLine); 

       // count the total number of lines 

       a++; 

      } 

     } 

     // excluding the number of lines that has comments and new lines 

     c = a - b; 

     // printing the number of lines excluding comments and new lines 

     System.out.println("Number of Lines are : " + c); 

     // printing the number of lines of the functions/method found inside 
     // the text file. 

     System.out.println("Number of Functions are : " + k); 

    } 

    catch (IOException e) { 

     System.out.println(e.getMessage()); 

    } 

    finally { 

     try { 

      // close bufferReader 

      if (br != null) { 

       br.close(); 

      } 

     } catch (IOException ex) { 

      System.out.println(ex.getMessage()); 

     } 

     } 

    } 
} 
+0

다음에 코드를 사용 블록,하지 인용. –

+0

@Kuba Rakoczy Thanx 많은 동생들. 편집 해 주셔서 감사합니다. –

답변

0

그것은 정규식 String.contains()를 사용할 수는 없습니다 : 그래서 여기 내 코드입니다. 당신은 아마뿐만 아니라 키워드를 제외해야

sCurrentLine.matches("[^()]+\\([^()]*\\)\\s*\\{?") 

(그래서 그들은 함수로 취급되지 않음) : 당신은 대신을 시도 할 수 있습니다

!sCurrentLine.matches("\\s*(if|switch|for|while|catch)\\s*\\(.+\\)") 
+0

Rakozy 정말 고마워요. 내가 할 수없는 또 하나는 "while()" "for()" "cathc"와 같은 다른 키워드를 배제하는 방법을 찾는다. 아이디어를 꺼내십시오. –

+0

답변을 수정 했으므로 언급 한 키워드는 제외됩니다. –

+0

Rakozy contains ("for) || contains ("while ") 등을 사용하여 제외했습니다. 그러나 새로운 문제가 발생했습니다. 여러 줄 주석을 제외하는 행이 작동하지 않습니다. 내 코드 if ((sCurrentLine ("/") && sCurrentLine.endsWith ("* /")) || sCurrentLine.startsWith ("//") || sCurrentLine.isEmpty() || (sCurrentLine.trim () .matches "[{}; \ n] +"))) { // 코멘트 라인의 수와 카운트에서 제외 할 새 라인 수를 카운트하십시오 b ++; –