SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Stuart Reges, Marty Stepp
Chapter:
File Processing
Exercise:
Exercises
Question:11 | ISBN:9780136091813 | Edition: 2

Question

Write a method called stripComments that accepts a Scanner representing an input file containing a Java program as
its parameter, reads that file, and then prints the file’s text with all comments removed. A comment is any text on a line
from // to the end of the line, and any text between /* and */ characters. For example, consider the following text:
import java.util.*;
/* My program
by Suzy Student */
public class Program {
public static void main(String[] args) {
System.out.println("Hello, world!"); // a println
}
public static /* Hello there */ void foo() {
System.out.println("Goodbye!"); // comment here
} /* */
}

If the file contained this text, your program should output the following text:
import java.util.*;
public class Program {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
public static void foo() {
System.out.println("Goodbye!");
}
}

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

package chap456;

import java.io.IOException;
import java.io.StringReader;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;

public class StripComments {
    
    
    public static String stripComments(String program) {
        StringBuilder newFile = new StringBuilder();    
        try (StringReader stringReader = new StringReader(program)) {
            
            boolean out = true;
            boolean BlockComment = false;
            boolean LineComment = false;
            
            int prev = stringReader.read();
            int cursor;
            for(cursor = stringReader.read(); cursor != -1; cursor = stringReader.read()) {
                if(BlockComment) {
                    if (prev == '*' && cursor == '/') {
                        BlockComment = false;
                        out = false;
                    }
                } else if (LineComment) {
                    if (cursor == '\r') { // start untested block
                        stringReader.mark(1);
                        int next = stringReader.read();
                        if (next != '\n') {
                            stringReader.reset();
                        }
                        LineComment = false;
                        out = false; // end untested block
                    } else if (cursor == '\n') {
                        LineComment = false;
                        out = false;
                    }
                } else {
                    if (prev == '/' && cursor == '*') {
                        stringReader.mark(1); // start untested block
                        int next = stringReader.read();
                        if (next != '*') {
                            BlockComment = true; // tested line (without rest of block)
                        }
                        stringReader.reset(); // end untested block
                    } else if (prev == '/' && cursor == '/') {
                        LineComment = true;
                    } else if (out){
                        newFile.append((char)prev);
                    } else {
                        out = true;
                    }
                }
                prev = cursor;
            }
            if (prev != -1 && out && !LineComment) {
                newFile.append((char)prev);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return newFile.toString();
    }

    public static void main(String[] args) throws IOException {
        String str = System.getProperty("user.dir") +"\\resources\\stripcomments.txt";
        Charset encoding = Charset.defaultCharset();
        List<String> lines = Files.readAllLines(Paths.get(str), encoding);
        String code = lines.stream().collect(Collectors.joining("\n"));
        
        System.out.println(stripComments(code));
    }

}
input file:  stripcomments.txt

/* My program
by Suzy Student */
public class Program {
public static void main(String[] args) {
System.out.println("Hello, world!"); // a println
}
public static /* Hello there */ void foo() {
System.out.println("Goodbye!"); // comment here
} /* */
}
Output:


public class Program {
public static void main(String[] args) {
System.out.println("Hello, world!"); }
public static  void foo() {
System.out.println("Goodbye!"); } 
}

 

0 0

Discussions

Post the discussion to improve the above solution.