Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: skipping counting of comments (lines)

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: skipping counting of comments (lines)

    Quote Originally Posted by KevinWorkman View Post
    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<=mi++)
                                      {
               
    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:


    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

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: skipping counting of comments (lines)

    Dear After compiling my code on SSCCE I got the following output:

    string:///CopyOfLineCountingProg.java:39: class, interface, or enum expected
    --- Update ---
    ^
    1 error
    Compiled without errors: false

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    Nov 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: skipping counting of comments (lines)

    Quote Originally Posted by KevinWorkman View Post
    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.

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Junior Member
    Join Date
    Nov 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: skipping counting of comments (lines)

    Quote Originally Posted by KevinWorkman View Post
    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....

Similar Threads

  1. [SOLVED] My program skipping a for loop
    By Dr.Code in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 16th, 2012, 09:05 AM
  2. Comments
    By igkarthi in forum Java Theory & Questions
    Replies: 1
    Last Post: January 27th, 2012, 07:52 AM
  3. [SOLVED] if else skipping
    By Scotty in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 25th, 2010, 08:04 PM
  4. Java error while using BufferedReader class to read a .txt document
    By Jchang504 in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: February 4th, 2009, 07:55 PM