Dear all
I tried to write a simple java code which has to do the following functions.
1. Getting an input file.
2. Couting number of lines and skipping the lines containing the following charachters:
;
}
//
{
Also if there are lines for which contains comments starting with /* and ending with */ it should be ignored from counting. Please help me correcting my code.
Thanks
/* Course: SE501 (Software Development Processes)
* PSP 0.1 process
* Program: Counting LOCs of a program excluding comments and lines which contain only {,},;,//.
*/
PHP Code:
import java.io.*;
public class CopyOfLineCountingProg
{
public static void main(String args[])
{
FileInputStream fis = null;
try
{
fis = new FileInputStream("d://dataset.txt");
}
catch(FileNotFoundException e)
{
System.out.println("The source file does not exist. " +e );
}
LineNumberReader lineCounter = new LineNumberReader(new InputStreamReader(fis));
String nextLine = null;
int ignoredLines = 0; // counter for lines to be excluded.
try {
while ((nextLine = lineCounter.readLine()) != null) {
System.out.println(nextLine);
if ((nextLine.startsWith("/*") && nextLine.endsWith("*/"))
|| (nextLine.trim().matches("[{};]+"))) {
//This line needs to be ignored
ignoredLines++;
continue;
}
}
System.out.println("Total number of line in this file " + lineCounter.getLineNumber());
int finalLines = 0; // for counting lines after exlcuding.
finalLines = lineCounter.getLineNumber()-ignoredLines ;
System.out.println("Total " +finalLines);
} catch (Exception done) {
done.printStackTrace();
}}}
--- Update ---
My program is not skipping lines starting with /* and ending with */. Please fix the problem I am new to java.