Click here to Skip to main content
15,914,500 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
it can not extract the comments like bellow
/*
this is the first program
*/

and it gives like
/*


*/

What I have tried:

package javaapplication5;


import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * BufferedReader and Scanner can be used to read line by line from any File or
 * console in Java.
 * This Java program demonstrate line by line reading using BufferedReader in Java
 *
 * @author Javin Paul
 */
public class BufferedReaderExample {   

    public static void main(String args[]) throws IOException{
      
        //reading file line by line in Java using BufferedReader       
        FileInputStream fis = null;
        BufferedReader br = null;
        //String line;
        try {
            fis = new FileInputStream("E:/Sum.txt");
            br = new BufferedReader(new InputStreamReader(fis));
            String line;
            while((line = br.readLine()) != null){
                if(line.contains("/*") || line.contains("*/")|| line.contains("//")){
                    
                    System.out.println(line);
                    
                }
                
                else
                   
                    System.out.println("");
                
            }
            br.close();
          
            
  }
        catch(IOException e){
            System.out.println("file could not read");
            
        }
    }
}
Posted
Updated 5-May-17 5:53am
Comments
[no name] 5-May-17 11:52am    
Yes, that is exactly what you told your program to do. What is your question?
Richard MacCutchan 5-May-17 12:00pm    
If the line contains "/*" then it may be a multi-line comment so you need to check if it also contains "*/", and if not keep looking on the following lines.
Member 13175373 5-May-17 16:13pm    
thank you i think you are right but my programming experience is not that much good
Richard MacCutchan 6-May-17 3:09am    
This is about simple logic rather than programming. If the line contains "//" then it is a single line comment, so that is easy to deal with. If the line contains "/*" then it is the start of a single or multi-line comment. So in this case you output the line, then check to see if it contains "*/". If it does then no more processing. If it does not, then set a flag to keep searching for the end of comment flag, and output each line until you find it.
Member 13175373 7-May-17 3:49am    
thank you i will try it

1 solution

The old, C-style comments can span more than one line so you could have a boolean flag that is true if you are in a comment and if so you need to output the lines within the comment.
 
Share this answer
 
Comments
Member 13175373 5-May-17 16:14pm    
please try it i will also try and share the sample thank you
Rick York 5-May-17 16:42pm    
No, I'm not going to try it but thanks anyway. I've written my own language and a debugger for it so I have had all the fun with parsing I need for two lifetimes.
PIEBALDconsult 5-May-17 17:48pm    
We need to be able to up-vote comments. +5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900