Advertisement
Guest User

Untitled

a guest
Oct 2nd, 2014
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. cloc src
  2.  
  3. package test;
  4.  
  5. public class Main {
  6.  
  7. /**
  8. * A javadoc comment.
  9. * @param args
  10. */
  11. public static void main(String[] args) {
  12. System.out.println("/* This isn't a comment */");
  13. /* This is a comment */
  14. //Comment
  15. System.out.println("//Not comment");
  16. }
  17. //Comment
  18. }
  19.  
  20. 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.
  21.  
  22. I count the empty lines, comment lines and lines of code separately.
  23.  
  24. if there is any sentence which has a some tabs or just spaces, they are counted as
  25. empty lines. They match with the regular expression s*
  26. s - any whitespace character (ntb)
  27. * - this means there can be any number of whitespace characters
  28.  
  29.  
  30. comment lines are the ones that match any of the following conditions :-
  31. 1) have a // outside of a string
  32. 2) have a /* some comment */
  33. 3) have a /* but the '*/' is not found on the same line
  34.  
  35. The regular expression for the above can be found inside the code!
  36.  
  37. 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)
  38.  
  39. The summary of these three types of lines are provided by this program.
  40.  
  41. //Below is the working code
  42.  
  43. import java.io.BufferedReader;
  44. import java.io.FileNotFoundException;
  45. import java.io.FileReader;
  46. import java.io.IOException;
  47.  
  48. public class CountComment {
  49.  
  50. public static void main(String[] args) {
  51. String line="";
  52. int count=0;
  53. try {
  54. BufferedReader br=new BufferedReader(new FileReader("./src/numbers/comment.txt"));
  55. while((line=br.readLine())!=null)
  56. {
  57. if(line.startsWith("//"))
  58. count++;
  59. if(line.startsWith("/*"))
  60. {
  61. count++;
  62. while(!(line=br.readLine()).endsWith("'*'"))
  63. {
  64. count++;
  65. break;
  66. }
  67. }
  68.  
  69. }
  70. br.close();
  71. }
  72. catch (FileNotFoundException e) {
  73. e.printStackTrace();
  74. }
  75. catch (IOException e) {
  76. e.printStackTrace();
  77. }
  78.  
  79. System.out.println("count="+count);
  80. }
  81.  
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement