How to calculate the number of lines of code within a program?
<p>I want to calculate the number of lines of code within a program, and for which I had already written code which counts the number of <code>;</code>. It works fine, but not in some situations like where there is no <code>;</code> present (like <code>if</code> and <code>while</code> statements). In this case I had stored some of the keywords in an array of strings and I want to search for that keyword by using <code>readLine()</code>. If it works fine then I will increment it by 1, but it's not working. I had tried a lot but it is not working at all, and it is showing the Exception. As <code>Demo.java</code> you can use your own code.</p>

<p><strong>Classdetect1.java</strong></p>

<pre><code>import java.io.*;
public class Classdetect1
{
    public static void main(String[] args) throws IOException
    {
        int i=0,j=0,k=0,p;
        String str1[]={"if","else","else if","while","for","goto","do"};

        // Open the file
        FileInputStream fstream = new FileInputStream("Demo.java");

        // Get the object of DataInputStream
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));

        String strLine;

        if (in == null){
            System.out.println("File is blank");
            System.exit(0);
        }

        while (i != -1)
        {
            i = in.read();
            if(i==';' || i=='{')
            {
                j=j+1;
            }

            if(i=='\'')
            {
                while(in.read()!='\'')
                    continue;
            }

            if(i=='\"')
            {
                while(in.read()!='\"')
                    continue;
            }

            if(i=='(')
            {
                while(in.read()!=')')
                    continue;
            }


            while ((strLine = br.readLine()) != null)
            {
                for(p=0;p&lt;7;p++)
                {
                    if(str[p].equals(strLine))
                    {
                        k=k+1;
                    }
                }
            }
            System.out.println("Line of =" + k);
        }
        System.out.println("Total Line of code=" + j);
    }
}
</code></pre>
