aspose file tools*
The moose likes Java in General and the fly likes How to find number of lines of code ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to find number of lines of code ?" Watch "How to find number of lines of code ?" New topic
Author

How to find number of lines of code ?

Muralidhar Gandepalli
Greenhorn

Joined: Apr 13, 2001
Posts: 13
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

Joined: May 17, 2001
Posts: 2823
Here is one. Do a search in google for "java lines of code counter" and you will find others.
Arnold king
Greenhorn

Joined: Sep 06, 2013
Posts: 10
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

Joined: Aug 16, 2005
Posts: 14272
    
  21

Welcome to the Ranch, Arnold king.

Note that we have code tags here that will format your code nicely. See: UseCodeTags


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Arnold king
Greenhorn

Joined: Sep 06, 2013
Posts: 10

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
Ranch Hand

Joined: Aug 27, 2012
Posts: 1067
    
  10

How may lines of code are there in this Java fragment?


and how many in this

Arnold king
Greenhorn

Joined: Sep 06, 2013
Posts: 10
19
Arnold king
Greenhorn

Joined: Sep 06, 2013
Posts: 10
19 and
1
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1067
    
  10

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

Joined: Jul 26, 2007
Posts: 10451
    
    8

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


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Arnold king
Greenhorn

Joined: Sep 06, 2013
Posts: 10
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
Ranch Hand

Joined: Aug 27, 2012
Posts: 1067
    
  10

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

Joined: Aug 16, 2005
Posts: 14272
    
  21

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).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to find number of lines of code ?