I want to create a linter java class that flag a word "break" as an error regardless of condition. Should return an error (with custom message) if the given line contains the break keyword outside of a single line comment (comments that start with //). i.e, we don't care about the word break inside comments, and only in the actual java code. Your check should only look for break specifically in all-lowercase (so occurrences of "Break" or "BReaK" outside of a single line comment should not be flagged). Note that this check is overly-simplistic in that it might flag some false uses of break such as System.out.println("break");. You do not need to handle this case specially; you should flag any use of the word break outside of a single line comment.
I want to create a linter java class that flag a word "break" as an error regardless of condition.
-
Should return an error (with custom message) if the given line contains the break keyword outside of a single line comment (comments that start with //). i.e, we don't care about the word break inside comments, and only in the actual java code.
-
Your check should only look for break specifically in all-lowercase (so occurrences of "Break" or "BReaK" outside of a single line comment should not be flagged).
-
Note that this check is overly-simplistic in that it might flag some false uses of break such as System.out.println("break");. You do not need to handle this case specially; you should flag any use of the word break outside of a single line comment.
-
Here's my current code:
import java.util.*;
public class BlankPrintlnCheck implements Check {
public Optional<Error> lint(String line, int lineNumber) {
if (line.contains("System.out.println(\"\")")) {
return Optional.of(new Error(3, lineNumber, "Line contains 'System.out.println(\"\")'"));
}
return Optional.empty();
}
}
currently it doesn't flag the word break when it's inside a comment:
Example: it doesn't flag this
//break
as an error
I need help.
Trending now
This is a popular solution!
Step by step
Solved in 2 steps