Note that there are some explanatory texts on larger screens.

plurals
  1. POJava - algorithm to find executable lines of code
    text
    copied!<p>I wrote a java program which scans and finds Executable lines of codes (ELOC), blank lines of codes(BLOC) and comments (CLOC) for only java and c++ codes. Following is my code:</p> <pre><code> if(extension.contains("java") || extension.contains("c++")) { Scanner input = new Scanner(fileObject); while(input.hasNext()) { String s = input.nextLine(); if(s.length()==0) { bloc++; } else if(s.contains("/*") || s.startsWith("/*")) { cloc++; while(!s.contains("*/")) { cloc++; s = input.nextLine(); } } else if(s.contains("//")) { cloc++; } else { eloc++; } }//while System.out.println("ELOC: "+(eloc)); System.out.println("Blank Lines: "+bloc); System.out.println("Comment Lines: "+cloc); } </code></pre> <p>I ran different java and c++ source codes but it does not always give the correct answer. What Can I do to make it better? Is there any java code online that I can use?</p> <p>For this question, I'm only counting the executable lines of codes. If a line looks like following:</p> <pre><code>int x=0;//some comment </code></pre> <p>then the line above should be counted as one executable line. Following is my updated code:</p> <pre><code>String extension=getExtension(fileObject.getName()); if(extension.contains("java") || extension.contains("c++")) { Scanner input = new Scanner(fileObject); String s; while(input.hasNext()) { s = input.nextLine().trim(); eloc++; if(s.equals("")) { bloc++; } if(s.startsWith("//")) { cloc++; } if(s.contains("/*") &amp;&amp; !s.contains("*\\")) { cloc++; while(!s.contains("*/")) { cloc++; eloc++; s = input.nextLine(); } } else if(s.contains("/*") &amp;&amp; s.contains("*\\")) { cloc++; } } System.out.println("Total number of lines: "+eloc); System.out.println("ELOC: "+(eloc-(cloc+bloc))); System.out.println("Blank Lines: "+bloc); System.out.println("Comment Lines: "+cloc); } </code></pre> <p>Any comment/advice will be appreciated..Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload