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&¬e3==-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&¬e3!=-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 ;
}
输出结果:
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&¬e3==-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&¬e3!=-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};
}
}
java 统计文件注释个数的更多相关文章
- Java统计文件数量
Java统计文件数量 package com.vfsd; import java.io.File; import java.io.IOException; /********************* ...
- Java的文件注释
以下内容引用自http://wiki.jikexueyuan.com/project/java/documentation.html: Java语言支持三种注释形式: 注释 描述 /*text*/ 编 ...
- python 统计文件的个数
import os path = r'F:\1back\picture' #获取当前路径 count = 0 for root,dirs,files in os.walk(path): #遍历统计 i ...
- Java统计文件中字母个数
import java.text.DecimalFormat; import java.io.File; import java.io.FileReader; import java.io.Buffe ...
- java统计文件字母大小写的数量练习
import java.io.*; import java.lang.*; public class WordStatistic { private BufferedReader br; privat ...
- JAVA统计中文的个数
尝试了一下 不错~ /** * 获取字符串的长度,中文占一个字符,英文数字占半个字符 * * @param value 指定的字符串 * @return 字符串的长度 */ public static ...
- 原创:C语言打开、下载、删除网页,统计网页字符个数
本程序由本人在华夏联盟的ID闪电笨笨原创,首发地址:http://bbs.hx95.com/ 写此程序希望可以可以激发新手学习C语言的积极性! C语言代码实现功能如下: 1.实现 ...
- python (9)统计文件夹下的所有文件夹数目、统计文件夹下所有文件数目、遍历文件夹下的文件
命令:os 用到的:os.walk os.listdir 写的爬虫爬的数据,但是又不知道进行到哪了,于是就写了个脚本来统计文件的个数 #统计 /home/dir/ 下的文件夹个数 import o ...
- Linux统计文件夹下文件信息
统计当前文件夹里面有多少文件,即统计文件个数 ls -l |grep "^-"|wc -l 统计当前文件夹里面有多少文件夹,即统计文件夹个数 ls -l |grep "^ ...
随机推荐
- BASE64 编码解码
/// <summary> /// Base64编码 /// </summary> /// <param name="data"></pa ...
- layui的单选框
<script type="text/html" id="radioTpl"> <input type="radio" n ...
- asp.net写日志权限问题
asp.net网站程序写不了日志,都是这个原因. 程序池的身份标识,设为内置的网络服务即可.
- iOS swift 富文本显示 富文本在iOS中使用场景和解决方案
项目中很多地方都会用到富文本的内容:比如一般的商品详情,视频详情,资讯详情等,运营人员通过后台的富文本编辑器编辑的内容,前端拿到的就是一段富文本的字符串,这富文本大多都是图片和文字的组合.我们今天介绍 ...
- WOW模型导出到Unity3D使用教程
最近又重操旧业.搞起了Unity3D.对WOW的模型怨念很深. 于是写了一个教程帮助其他人一起提取wow的模型..哈哈..希望能帮助到大家哈哈.. 我自己的百度网盘里面附加.有兴趣的大家下载看看.文档 ...
- nginx 403 forbidden 二种原因
nginx 403 forbidden 二种原因 引起nginx 403 forbidden有二种原因,一是缺少索引文件,二权限问题.今天又遇到 了,顺便总结一下. 1,缺少index.html或者i ...
- C++ 函数模板一(函数模板定义)
//函数模板定义--数据类型做参数 #include<iostream> using namespace std; /* 函数模板声明 1.函数模板定义由模板说明和函数定义组成,并且一个模 ...
- 目标跟踪之卡尔曼滤波---理解Kalman滤波的使用预测
Kalman滤波简介 Kalman滤波是一种线性滤波与预测方法,原文为:A New Approach to Linear Filtering and Prediction Problems.文章推导很 ...
- Java集合框架:Collections工具类
java.util.Collections工具类提供非常多实用的方法.使得程序员操作集合类的时候更加的方便easy,这些方法都是静态的. 整个Collections工具类源代码几乎相同有4000行.我 ...
- curl使用例子
地址:http://phpbook.phpxy.com/34771 参考:http://php.net/manual/zh/function.curl-setopt.php 我们将curl的步骤分为以 ...