1

I have written the following program to read from a file and skip comments, it works for single line comments, but not for multi line ones. Does anyone know why? I don't need to worry about "//" in Strings. And only java comments ie "//" and "/* */"

code:

import java.io.*;

public class IfCounter2 
{   
    public static boolean lineAComment(String line)
    {
        if (line.contains("//"))    
            return true;

        return false;
    }

    public static boolean multiLineCommentStart(String line)
    {
        if (line.contains("/*"))    
            return true;

        return false;
    }

    public static boolean multiLineCommentEnd(String line)
    {
        if (line.contains("*/"))    
            return true;

        return false;
    }

    public static void main(String[] args) throws IOException 
    {
        String fileName = args[0];

        int numArgs = args.length;

        int ifCount = 0;

        // create a new BufferReader
        BufferedReader reader = new BufferedReader(new FileReader(fileName));
        String line = null;
        StringBuilder stringBuilder = new StringBuilder();
        String ls = System.getProperty("line.separator");

        line = reader.readLine();
        // read from the text file
        boolean multiLineComment = false;

        while (( line = reader.readLine()) != null) 
        {
          if (!multiLineCommentStart(line)) 
          {
            multiLineComment = true;
          }

          if (multiLineComment) {
            if (!multiLineCommentEnd(line)) 
            {
              multiLineComment = false;
            }
          }

          if (!lineAComment(line) && !multiLineComment) 
          {
            stringBuilder.append(line);
            stringBuilder.append(ls);
          }
        }


        // create a new string with stringBuilder data
        String tempString = stringBuilder.toString();
        System.out.println(stringBuilder.toString());

    } 
} 
7
  • How are you using these methods? it would seem that the problem is in the code that implements this unless I misunderstand the question. Commented Sep 13, 2011 at 3:00
  • In the main just below the methods :) Commented Sep 13, 2011 at 3:03
  • Sorry, didnt see the scrollbar... :/ Commented Sep 13, 2011 at 3:08
  • No problem. What do you think now that you see implementation? Commented Sep 13, 2011 at 3:14
  • I put my response as a comment on Matt's answer. Commented Sep 13, 2011 at 3:20

3 Answers 3

2

You only set multiLineComment to true when !multiLineCommentStart(line) is true - that is, whenever the line does not contain /*.

Sign up to request clarification or add additional context in comments.

6 Comments

So what is it exactly that needs to be changed?
It seems that the intention of multiLineComment is to track when you are in between the start and end of a multiline comment. So think about when it makes sense to flip that flag active - when should you start tracking that you are in a multiline comment? Hint: it is not when the line does not contain the multiline start.
So i should get rid of the "!" not. So then i start tracking it when the line begins with "/*" ?
@josh. pretty sure thats right. If you remove the "!" from your first two if statements (with the references to the multiLineComment methods), right now you are are setting your variable to the opposite value than you want.
@Andy. That gets rid of everything but the last */ (closing multi line) So close. Any idea why?
|
0

Basically, your code should look sth like this (untested)

    boolean multiLineComment = false;

    while (( line = reader.readLine()) != null) 
    {
      if (multiLineCommentStart(line)) 
      {
        multiLineComment = true;
      }

      if (multiLineComment) {
        if (multiLineCommentEnd(line)) 
        {
          multiLineComment = false;
        }
      }

      if (!lineAComment(line) && (multiLineComment == false)) 
      {
        stringBuilder.append(line);
        stringBuilder.append(ls);
      }
    }

in that last if statement, you need to have an expression with your variable and a fixed

Comments

0

Andy's answer is right on the money but needs a validation in last if to make sure you are not counting */ as a valid line:

    boolean multiLineComment = false;

    while (( line = reader.readLine()) != null) 
    {
      if (multiLineCommentStart(line)) 
      {
        multiLineComment = true;
      }

      if (multiLineComment) {
        if (multiLineCommentEnd(line)) 
        {
          multiLineComment = false;
        }
      }

      if (!lineAComment(line) && (multiLineComment == false) &&
           !multiLineCommentEnd(line) ) 
      {
        stringBuilder.append(line);
        stringBuilder.append(ls);
      }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.