Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to find number of lines of code ?

 
Muralidhar Gandepalli
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All,I have a requirment to find the number of lines( we have to exclude blnak lines and comments) written in a project (written in Java ).
We can write a program to do this i just wanted to check if any tool (off shelf) available to do this ?
Thanx in advance
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is one. Do a search in google for "java lines of code counter" and you will find others.
 
Arnold king
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class TestLines {
private static int lineCount;
public static void main(String[] args) {
Scanner s = null;
try {
File file = new File("src\\com\\TestLines.java");
s = new Scanner(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while (s.hasNext()) {
String string = (String) s.nextLine();
if (string != null) {
lineCount++;
}
}
System.out.println(lineCount);
}
}

Above program gives number of lines of code in your file(give the correct path of the file)
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15272
37
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Arnold king.

Note that we have code tags here that will format your code nicely. See: UseCodeTags
 
Arnold king
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thank you Jesper de Jong for information and i am not able to see edit option to edit/delete my old post.how to do that ?
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How may lines of code are there in this Java fragment?


and how many in this

 
Arnold king
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
19
 
Arnold king
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
19 and
1
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arnold king wrote:19 and
1


In other words to use the number of lines of code as a metric is probably meaningless !
 
Maneesh Godbole
Saloon Keeper
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard Tookey wrote:
In other words to use the number of lines of code as a metric is probably meaningless !

Not if you are getting paid by lines of code
 
Arnold king
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard Tookey wrote:
Arnold king wrote:19 and
1


In other words to use the number of lines of code as a metric is probably meaningless !


you are right!! i just posted the code which gives number of lines in a file.
and not support to use the number of lines of code as a metric
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:
Richard Tookey wrote:
In other words to use the number of lines of code as a metric is probably meaningless !

Not if you are getting paid by lines of code


Many many years ago as an exercise I wrote the whole of a radix 2 FFT, including the resort, in APL in just 1 line of indecipherable code. It took about a day but the next day even I could not work out how it worked! At that time it was reckoned that the average programmer produced between 3 and 5 lines of fully documented code per day so my 1 line FFT was well bellow average since I had produced no documentation. Now I could have broken that 1 line into about 50 lines and then half an hour of documentation would have meant I wrote 50 odd lines of fully documented code in that particular day.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15272
37
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arnold king wrote:Thank you Jesper de Jong for information and i am not able to see edit option to edit/delete my old post.how to do that ?

There should be an button on the top right of your post, but there might be certain rules like you have to edit it within a certain amount of time (and then the edit button will not be there anymore).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic