参考: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 ;
}

输出结果:

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

java 统计文件注释个数的更多相关文章

  1. Java统计文件数量

    Java统计文件数量 package com.vfsd; import java.io.File; import java.io.IOException; /********************* ...

  2. Java的文件注释

    以下内容引用自http://wiki.jikexueyuan.com/project/java/documentation.html: Java语言支持三种注释形式: 注释 描述 /*text*/ 编 ...

  3. python 统计文件的个数

    import os path = r'F:\1back\picture' #获取当前路径 count = 0 for root,dirs,files in os.walk(path): #遍历统计 i ...

  4. Java统计文件中字母个数

    import java.text.DecimalFormat; import java.io.File; import java.io.FileReader; import java.io.Buffe ...

  5. java统计文件字母大小写的数量练习

    import java.io.*; import java.lang.*; public class WordStatistic { private BufferedReader br; privat ...

  6. JAVA统计中文的个数

    尝试了一下 不错~ /** * 获取字符串的长度,中文占一个字符,英文数字占半个字符 * * @param value 指定的字符串 * @return 字符串的长度 */ public static ...

  7. 原创:C语言打开、下载、删除网页,统计网页字符个数

    本程序由本人在华夏联盟的ID闪电笨笨原创,首发地址:http://bbs.hx95.com/ 写此程序希望可以可以激发新手学习C语言的积极性! C语言代码实现功能如下:            1.实现 ...

  8. python (9)统计文件夹下的所有文件夹数目、统计文件夹下所有文件数目、遍历文件夹下的文件

    命令:os 用到的:os.walk   os.listdir 写的爬虫爬的数据,但是又不知道进行到哪了,于是就写了个脚本来统计文件的个数 #统计 /home/dir/ 下的文件夹个数 import o ...

  9. Linux统计文件夹下文件信息

    统计当前文件夹里面有多少文件,即统计文件个数 ls -l |grep "^-"|wc -l 统计当前文件夹里面有多少文件夹,即统计文件夹个数 ls -l |grep "^ ...

随机推荐

  1. atitit. 管理哲学 大毁灭--- 如何防止企业的自我毁灭

    atitit. 管理哲学 大毁灭---  如何防止企业的自我毁灭 1. 为什么企业组织的生命力 普遍不如国家组织的长久 2 2. 企业的不稳定因子如下:: 2 3. 决策制度 2 3.1. 我们老大说 ...

  2. CentOS上扩充lv-root空间步骤详解

    查看服务器发现vg_host01-lv_root下的空间占用的比较多,需要扩容. 有以下两种方案: )利用空余的磁盘,扩展lv_root的大小(推荐) )将lv_home的空间挪出一部分给lv_roo ...

  3. HDU 1020 Encoding 模拟

    Encoding Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  4. 缓存数据库redis、memcached。 MongoDB 资源集锦

    Redis学习资源: 1.Redis学习手册 http://www.cnblogs.com/stephen-liu74/archive/2012/03/14/2349815.html 2.c#连接Re ...

  5. 使用randA()生成randB()

    randA()表示可以随机生成1--A的整数 rand7()生成rand5() int Rand5(){ int x = ~(1<<31); // max int while(x > ...

  6. HBase学习笔记——配置及Shell操作

    1.HBase的配置 还是以前配置的集群,见:http://www.cnblogs.com/DarrenChan/p/6493373.html 我们约定:weekend03和weekend04放HMa ...

  7. 流媒体服务器开发笔记(2)--RTCP协议介绍

    http://blog.sina.com.cn/s/blog_53061af00100o2no.html ——————————————————————————————————————————————— ...

  8. import _mysql----ImportError: DLL load failed: %1 不是有效的 Win32 应用程序。

    背景:安装了mysql,练习sql 操作,提示 ImportError DLL load failed: %1 不是有效的 Win32 应用程序 解决方法: 操作系统win10,64位,查看安装的my ...

  9. Using Fast Weights to Attend to the Recent Past

    Ba, Jimmy, et al. "Using Fast Weights to Attend to the Recent Past." Advances In Neural In ...

  10. 初识Python、PyCharm、Anaconda与tensorflow

    最近裸辞了,未来希望转深度学习.语音识别.文本挖掘,觉得这块特别有意思,比较好玩.开始自学相关知识,为了能够独立地.系统地了解和学习相关知识,计划不定期记录和更新一些平时的学习总结,个人关于以上几个方 ...