Search code examples
javarpn

Reverse Polish Notation Challenge - Boolean.getBoolean() vs. Boolean.parseBoolean()


I am trying to solve this question: https://uchicago.kattis.com/problems/uchicago.rpn

I've posted the Java code below for reference.

EDIT: I've removed the original code as it was too long and not too helpful. I think this post can be useful for future readers, if it can serve as a practical example that is able to underscore the difference between getBoolean() and parseBoolean() functions in Java. Further clarification can be found here. The bug turned out to be related to getBoolean() vs. parseBoolean() as can be seen from the code below, in which val1 and val2 were of the boolean types, as opposed to string names.

boolean bool1 = Boolean.getBoolean(val1);// Should be using parseBoolean(val1)
boolean bool2 = Boolean.getBoolean(val2);// Should be using parseBoolean(val2)
                        
String result = booleanHelper(bool1, bool2, next);

Solution

  • After further testing I think the only error you have is parsing the boolean values "true" and "false" with Boolean.getBoolean instead of Boolean.parseBoolean. that results in false answers with inputs like true true and (answer: false).

    Boolean.getBoolean will read the system property with given name and try to parse that as boolean, not actually your strings "true" and "false".