want to create a linter java class that flag a word "break" as an error. 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 BreakCheck implements Check { public Optional lint(String line, int lineNumber) { // Check if the line contains the word "break" in all-lowercase if (line.matches("(?i).*\\bbreak\\b.*") && !line.matches("(?s).*//.*\\bbreak\\b.*")) { return Optional.of(new Error(2, lineNumber, "Line contains 'break' outside of a single line comment")); } return Optional.empty(); }
I want to create a linter java class that flag a word "break" as an error.
-
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 BreakCheck implements Check {
public Optional<Error> lint(String line, int lineNumber) {
// Check if the line contains the word "break" in all-lowercase
if (line.matches("(?i).*\\bbreak\\b.*") && !line.matches("(?s).*//.*\\bbreak\\b.*")) {
return Optional.of(new Error(2, lineNumber, "Line contains 'break' outside of a single line comment"));
}
return Optional.empty();
}
}
It flag Break as error which shouldn't happen because it starts with capital B. I need help with this bug.
Trending now
This is a popular solution!
Step by step
Solved in 2 steps