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:
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);
}
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?
For this question, I'm only counting the executable lines of codes. If a line looks like following:
int x=0;//some comment
then the line above should be counted as one executable line. Following is my updated 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("/*") && !s.contains("*\\"))
{
cloc++;
while(!s.contains("*/"))
{
cloc++;
eloc++;
s = input.nextLine();
}
}
else if(s.contains("/*") && 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);
}
Any comment/advice will be appreciated..Thanks!