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!");
}
}
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!"); }
}