Your approach is mostly correct, but it has the fallacies of encompassing the entire logic and gives no meaningful names/explanations to the flow, variables etc. a more robust, extensible, clear and maintainable solution would be to replace all constants with final variables and encapsulate small problem domains into their own methods. For exmaple:
public static final String PLUS = "+";
public static final String MINUS = "-";
...
if (isStudentNameLine(String line)) {
processStudentNameLine(String line);
} else {
...
private static boolean isStudentNameLine(String line) {
return !line.startsWith(PLUS) && !line.startsWith(MINUS);
}
private static void processStudentNameLine(String line) {
System.out.print(line);
}
This may seem like an overkill to replace one lines of codes with methods. There are many advantages to this approach:
- using variables instead of constants mean you dont have to deal with typos
- smaller methods are easier to debug
- the code becomes clearer