I'm trying to figure out why this piece of code is not working, as the notation "156 154 152 - 3 + -" should go through without throwing an exception. Is there maybe a better way to use regex in this case? When I actually run my interpret function without manually throeing the exception, the result is correct and all is good. But for this exercise, there is a requirement for such exception handling.
Here's the code:
public class RegexTest {
public static void main(String[] arg) {
boolean b = check_notation("156 154 152 - 3 + -");
System.out.println(b);
}
public static boolean check_notation(String pol){
pol = pol.trim();
String[] tokens = pol.split("\\s+");
for (String r : tokens) {
if (!(r.matches("-[0-9]*") || r.matches("[0-9]*") || r == "/" || r == "*" || r == "-" || r == "+")) {
throw new RuntimeException("There are symbols that are not allowed.");
}
}
return true;
}
}