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 "^ ...
随机推荐
- PHP获得数组的交集与差集
PHP获得数组的交集与差集 作者:简明现代魔法图书馆 发布时间:-- :: 次阅读 服务器君一共花费了218. ms进行了6次数据库查询,努力地为您提供了提供了这个页面. 数组的交集 array_in ...
- weex-iOS集成
weex-iOS集成 weex只是刚刚起步,还存在一些bug,有些功能还有待完善和提高.但是其使用起来还是可以节省些时间. 这里我们说说如何把weex集成到我们的iOS项目中 1. 下载weex源代码 ...
- locationInView和translationInView的区别
1 translationInView是UIPanGestureRecognizer下面的一个属性 locationInView则是UIGestureRecognizer下面的属性 2 tra ...
- 如何输出long double?
首先long double是C99引入的,但是如何printf格式化一个long double的数据的呢? scanf一个double数据,是%lf,printf一个float或者double都是%f ...
- PHP学习笔记(5)GD库画验证码
<?php header("content-type:image/png"); $width = 500; $height = 500; $img = imagecreate ...
- mysql中文排序问题
mysql中文排序,用到的是: SELECT id id, billId billId, namespec nameSpec, unit unit, amount amount, price pric ...
- Flask 安装 快速入门
$ pip install flask Flask自带的Server在端口5000上监听: ython app.py flask通过request.form['name']来获取表单的内容. 外部可见 ...
- HttpClient+jsoup登录+解析 163邮箱
找了几个,只有这个靠谱,用的是httpclient4,另外还需要commons-lang和jsoup包 http://jsoup.org/ http://www.oschina.net/code/sn ...
- jxta-amalto
前几天在网络上搜索jxta的消息,发现jxta 2.8x已经启动了,官方地址http://chaupal.github.io/ 在浏览其邮件列表时,意外发现一老外基于jxta 2.6修改的一版, 可在 ...
- 封装常用的selenium方法
package com.yk.userlive.base; import java.net.MalformedURLException;import java.net.URL;import java. ...