skipping counting of comments (lines)
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.
Re: skipping counting of comments (lines)
Can you give us an SSCCE that demonstrates an example of the String you're reading in. We don't have the file, so just hardcoding it into a String variable is fine. Also we don't need any of the extra counting logic, just the true or false of whether the String starts with and ends with those characters.
Re: skipping counting of comments (lines)
Quote:
Originally Posted by
KevinWorkman
Can you give us an
SSCCE that demonstrates an example of the String you're reading in. We don't have the file, so just hardcoding it into a String variable is fine. Also we don't need any of the extra counting logic, just the true or false of whether the String starts with and ends with those characters.
Thank you so much dear for your quick reply. I dont know what is SSCCE but here are some of my work:
I am using Eclipse software.
I am trying to read a file naming "Dataset.txt". The Dataset.txt contains the following Code lines.
PHP Code:
/* comments to be ignored */
// lines to be ignored
public class DataSetTester {
public static void main(String[] args)
{
DataSet a = new DataSet();
Scanner input=new Scanner(System.in);
System.out.println("enter the total number of items");
int m =input.nextInt();
for(int i =1; i <=m; i++)
{
System.out.println("enter the "+i+" number");
a.add(input.nextInt());
}
System.out.println("count: " + a.getCount());
System.out.println("Mean: " + a.getMean());
System.out.println("standard deviation: " + a.getStandardDeviation());
}
}
The total LOC could be seen here:
http://i16.photobucket.com/albums/b5...creenshot1.jpg
Total 26 LOCs.
The following line no. should be ignored:
Line 1 - /* comments to be ignored */
Line 2 - White Space
Line 3 - // lines to be ignored
Line 4 - White Space
Line 7- {
Line 13 - {
Line 16 - }
Line 17 - White Space
Line 18 - Whtie Space
Line 20 - White Space
Line 22 - White Space
Line 24 - White Space
Line 25 - }
Line 26 - }
After Running the program I got the following Output:
Total number of line in this file 26
Total 21
The output shows that it only ingnored lines containing } and {.
// line starting and ending with /* and */ and white spaces were counted.
For White Space counting I didnt put code.
Your are Right !!!
I need a logic to simply ignore counting lines containing the characters to be ignored.
Thanks
Re: skipping counting of comments (lines)
I linked to the page on an SSCCE- basically, debugging starts with boiling your problem down to a single issue. You have quite a few things going on in your program, so it's hard to figure out which part of that is the actual issue.
For example, does your loop through the lines work? If so, then you can take that out of the SSCCE and simply use a single hardcoded String value. Which part of the if statement is giving you trouble? Test them one at a time to figure it out. If all of them work, perhaps the problem is with your arithmetic somewhere.
When you get it boiled down to an SSCCE of only a few lines, then we can help with your misunderstanding.
Re: skipping counting of comments (lines)
Dear After compiling my code on SSCCE I got the following output:
Quote:
string:///CopyOfLineCountingProg.java:39: class, interface, or enum expected
--- Update ---
^
1 error
Compiled without errors: false
Re: skipping counting of comments (lines)
That means you have a compiler error. Without seeing the SSCCE, I really can't tell you what's going on, other than something's wrong with your syntax.
Re: skipping counting of comments (lines)
Quote:
Originally Posted by
KevinWorkman
That means you have a compiler error. Without seeing the
SSCCE, I really can't tell you what's going on, other than something's wrong with your syntax.
Thank you so much dear even I dont know how to use SSCCE. I just compiled my software in the SSCCE software and got the output.
The problem is in the following code
PHP Code:
if ((nextLine.startsWith("/*") && nextLine.endsWith("*/"))
|| (nextLine.trim().matches("[{};]+")))
It was supposed that it counts the lines starting with /* and ending with */. But the counter ignoredLines++ is not counting comments in the input file.
Re: skipping counting of comments (lines)
I don't know what you're saying. An SSCCE is simply a smaller version of your program. You have a lot of extra stuff in there, all I'm asking is for you to eliminate that so we can focus on one thing at a time. You've boiled it down to one line, now it becomes your job to put together a sample program showing how that line doesn't work. Which part of that line doesn't work, since there are several pieces working together? What is the input that doesn't work- hard code it.
I predict that one of your assumptions about what the line starts with, ends with, or matches is not valid. The best way to do that is to make a small program that really examines what's going on.
Re: skipping counting of comments (lines)
Quote:
Originally Posted by
KevinWorkman
I don't know what you're saying. An SSCCE is simply a smaller version of your program. You have a lot of extra stuff in there, all I'm asking is for you to eliminate that so we can focus on one thing at a time. You've boiled it down to one line, now it becomes your job to put together a sample program showing how that line doesn't work. Which part of that line doesn't work, since there are several pieces working together? What is the input that doesn't work- hard code it.
I predict that one of your assumptions about what the line starts with, ends with, or matches is not valid. The best way to do that is to make a small program that really examines what's going on.
Your are extremely right. Sorry for wasting your time I will try....