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 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&¬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 統計文件註釋個數