鍍金池/ 問答/Java  Python/ Java統(tǒng)計文件注釋個數(shù)和注釋字符數(shù)

Java統(tǒng)計文件注釋個數(shù)和注釋字符數(shù)

我把文件一行一行地存入到了動態(tài)數(shù)組 list中,然后刪除list里面的注釋和輸出語句。其中countNote是算注釋個數(shù)的,charInNote是算注釋的總字符數(shù)的??墒亲詈蟮玫降膌ist存在很多問題,有大佬能幫我改一下代碼嗎
這是測試文件String.java

注釋總個數(shù)應(yīng)該有156個
注釋總的字符數(shù)應(yīng)該有 88469個

public void operateNote(ArrayList<String> list) throws Exception{
        
         String s = null;
        for(int j=0;j<list.size();j++) {
            s=list.get(j);
            int note1=s.indexOf("/*");
            int note2=s.indexOf("http://");
            int note3=s.indexOf("*/");
            //int note4=s.indexOf("\"");

            String dm="\"(.*)\"";//雙引號
            String sm="\'(.*)\'";//單引號                 
            
           if(note1!=-1&&note3==-1) {//多行注釋
               countNote++;
               String ttt=list.get(j);
               list.set(j, ttt.substring(0, note1));
               charInNote+=s.substring(note1).length()+1;//+1是包括換行符
                             
                   s=list.get(++j);
               while((note3=s.indexOf("*/"))==-1) {
                     if((note2=s.indexOf("http://"))!=-1) {
                         countNote++;
                     }
                     list.remove(j);
                     
                   charInNote+=s.length()+1;
                   if(j<list.size()-1) {
                       s=list.get(++j);
                   }else {
                       break;
                   }
               }
               list.remove(j);
               charInNote+=s.length();
               
           }else if(note2!=-1) {// "http://"類的單行注釋
               countNote++;
               list.set(j, s.substring(0,note2));
               charInNote+=s.substring(note2).length()+1;
           }else if(note1!=-1&&note3!=-1) {//單行注釋
               countNote++;

               String m1=s.substring(0, note1);
               String m2=s.substring(note3+2);
               String m3=m1+m2;
               charInNote+=s.substring(note1, note3+2).length();
               list.set(j, m3);
           }else {//刪除輸出語句
               String rp=list.get(j);
               rp=rp.replaceAll(dm, "");
               list.set(j, rp);
           }
           
        }


    }
回答
編輯回答
夏夕

你這個對與邏輯不是很清晰:
應(yīng)該是這樣:
字符串首先trim,去除前后空格
先判斷/**
如果是,記錄多行狀態(tài)為true,累加注釋個數(shù)
在此期間的//忽略,累加注釋字符數(shù)
再判斷*/,如果是,記錄多行狀態(tài)為false,
判斷//,如果是,記錄//到字符串尾的字符數(shù).累加注釋字符數(shù),累加注釋個數(shù)
還有,我在枚舉中喜歡 /* 注釋 / 這樣考慮,你也要注意下.

2017年2月12日 11:57
編輯回答
離殤

把list.remove(j)改成list.set(j,"")即可

2018年8月22日 09:00
編輯回答
呆萌傻

用正則表達式的方法太粗陋了,我覺得你可以試一下StreamTokenizer,可以自動解析注釋、字符串等

2018年6月29日 07:28
編輯回答
維她命

統(tǒng)計代碼行數(shù)有很多現(xiàn)成的輪子

cloc -- Count Lines of Code

如果你用linux,可以類似的方式安裝

 $sudo apt install cloc

在我的某個項目下執(zhí)行cloc得到的結(jié)果

$ cloc .
     767 text files.
     738 unique files.                                          
    6056 files ignored.

http://cloc.sourceforge.net v 1.60  T=2.22 s (327.1 files/s, 47582.5 lines/s)
-----------------------------------------------------------------------------------
Language                         files          blank        comment           code
-----------------------------------------------------------------------------------
Java                               716          16206          18073          69344
XML                                  6            107             62           1321
Maven                                1             29             19            290
Groovy                               1             10              0             12
Visualforce Component                1              0              0              6
-----------------------------------------------------------------------------------
SUM:                               725          16352          18154          70973
-----------------------------------------------------------------------------------

如果你寫這個程序僅是為了練手,還是自己想辦法完善才能練出來,大佬們忙打怪呢

2017年8月24日 11:14