Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- cloc src
- package test;
- public class Main {
- /**
- * A javadoc comment.
- * @param args
- */
- public static void main(String[] args) {
- System.out.println("/* This isn't a comment */");
- /* This is a comment */
- //Comment
- System.out.println("//Not comment");
- }
- //Comment
- }
- Note :- This program is not optimized for performance, performance is not an attribute i care about here. I chose not to use Pattern.compile() among many other decisions.
- I count the empty lines, comment lines and lines of code separately.
- if there is any sentence which has a some tabs or just spaces, they are counted as
- empty lines. They match with the regular expression s*
- s - any whitespace character (ntb)
- * - this means there can be any number of whitespace characters
- comment lines are the ones that match any of the following conditions :-
- 1) have a // outside of a string
- 2) have a /* some comment */
- 3) have a /* but the '*/' is not found on the same line
- The regular expression for the above can be found inside the code!
- Lines of code are the ones that end in ')' , '}' , '{' or ';' (one problem with this approach is that it does not count lines that have code followed by comment as a line of code)
- The summary of these three types of lines are provided by this program.
- //Below is the working code
- import java.io.BufferedReader;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- public class CountComment {
- public static void main(String[] args) {
- String line="";
- int count=0;
- try {
- BufferedReader br=new BufferedReader(new FileReader("./src/numbers/comment.txt"));
- while((line=br.readLine())!=null)
- {
- if(line.startsWith("//"))
- count++;
- if(line.startsWith("/*"))
- {
- count++;
- while(!(line=br.readLine()).endsWith("'*'"))
- {
- count++;
- break;
- }
- }
- }
- br.close();
- }
- catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- System.out.println("count="+count);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement