java 统计文件注释个数

参考:https://segmentfault.com/q/1010000012636380/a-1020000012640905

题目:统计文件中//和/* */注释的个数,双引号中的不算

import java.util.ArrayList;
import java.util.Scanner;

public class NoteCounter {

    public static void main(String[] args) throws Exception {
        // TODO 自动生成的方法存根
        Scanner in=new Scanner(System.in);
        ArrayList<String> ve=new ArrayList<String>();
        while(in.hasNext()){
            String temp=in.nextLine();
            ve.add(temp);
            if(temp.equals("}"))break;
        }
        System.out.println(ve);
        System.out.println(operateNote(ve)[0]+"  "+operateNote(ve)[1]);
    }
    public static int[] operateNote(ArrayList<String> list) throws Exception{
        
       String s = null;
       int countNote=0;
       int charInNote=0;
       for(int j=0;j<list.size();j++) {
           s=list.get(j);
           int note1=s.indexOf("/*");
           int note2=s.indexOf("//");
           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("//"))!=-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) {// "//"类的单行注释
              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);
          }
          
       }
       return new int[]{countNote,charInNote};

   }

}

测试数据:

// line comment //
/* block comment */ /* block comment2 */
int main(){
    char[] s="/* string */";
    return 0;
}

输出结果:

3(注释个数)   20(注释中字符的个数)

package toutiao;
import java.util.ArrayList;import java.util.Scanner;
public class NoteCounter {
public static void main(String[] args) throws Exception {// TODO 自动生成的方法存根Scanner in=new Scanner(System.in);        ArrayList<String> ve=new ArrayList<String>();        while(in.hasNext()){        String temp=in.nextLine();        ve.add(temp);        if(temp.equals("}"))break;        }        System.out.println(ve);        System.out.println(operateNote(ve)[0]+"  "+operateNote(ve)[1]);}public static int[] operateNote(ArrayList<String> list) throws Exception{               String s = null;       int countNote=0;       int charInNote=0;       for(int j=0;j<list.size();j++) {           s=list.get(j);           int note1=s.indexOf("/*");           int note2=s.indexOf("//");           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("//"))!=-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) {// "//"类的单行注释              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);          }                 }       return new int[]{countNote,charInNote};
   }
}

猜你喜欢

转载自www.cnblogs.com/zealousness/p/9028386.html