java 文件数_java 统计文件注释个数

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

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

importjava.util.ArrayList;importjava.util.Scanner;public classNoteCounter {public static void main(String[] args) throwsException {//TODO 自动生成的方法存根

Scanner in=newScanner(System.in);

ArrayList ve=new ArrayList();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 list) throwsException{

String s= null;int countNote=0;int charInNote=0;for(int j=0;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

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*/

intmain(){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 ve=new ArrayList();        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 list) throws Exception{               String s = null;       int countNote=0;       int charInNote=0;       for(int j=0;j

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

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};

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值