我想计算一个程序内的代码行数,并且我已经编写了代码来计算;
的数量。它工作正常,但在某些情况下不存在;
(如if
和while
语句)。在这种情况下,我将一些关键字存储在一个字符串数组中,我想使用readLine()
来搜索该关键字。如果它工作正常,那么我会增加1,但它不起作用。我尝试了很多,但它根本不工作,它显示了异常。由于Demo.java
您可以使用自己的代码。如何计算程序中的代码行数?
Classdetect1.java
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");
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<7;p++)
{
if(str[p].equals(strLine))
{
k=k+1;
}
}
}
System.out.println("Line of =" + k);
}
System.out.println("Total Line of code=" + j);
}
}
什么是_The EXCEPTION_,以及在哪里呢_it_发生的呢? – Virtlink 2013-04-21 12:17:58
考虑写一个解析器,读一下Antlr – 2013-04-21 12:21:01
'str [p] .equals(strLine)',你在哪里定义了名为'str'的数组? – NINCOMPOOP 2013-04-21 12:22:41