I am working on a homework assignment for which I need to read a java source file and remove all the comments from it. The rest of the styling should stay the same.
I have already completed the task by using regex.
But I am looking to accomplish the same without using regex.
Example Input
// My first single line comment
class Student {
/* Student class - Describes the properties
of the student like id, name */
int studentId; // Unique Student id
String studentName; // Name of the student
String junk = "Hello//hello/*hey";
} // End of student class
Result
class Student {
int studentId;
String studentName;
String junk = "Hello//hello/*hey";
}
My idea is to read each line
1) Check the first two characters
if it starts with // ==> remove the line
if it starts with /* ==> remove all the lines till */
2) Another scenario is handling
Example - int studentId; // Comment or /* Comment */
Can someone provide a better approach to this ?