-4

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);
Mo Aboulmagd
  • 131
  • 2
  • 10
  • 2
    Have you tried debugging your code? You're really asking us to do a lot of work that you should be doing yourself. You're even making us hunt down the test case. Sorry, that's not how StackOverflow works. – ajb Dec 08 '17 at 07:19
  • Without knowing what "test case #7" is, we really can't help you. And if we knew then you would know as well, and would be able to [debug your own program](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) with that specific input to find out what, when and where it goes wrong. – Some programmer dude Dec 08 '17 at 07:19
  • @Someprogrammerdude Yup, unfortunately, I cannot even access test case #7. – Mo Aboulmagd Dec 08 '17 at 07:21
  • 1
    Your code seems to work with the 5 sample inputs, but I noticed some things that can be improved (not sure if that solves your test case #7, probably not related). First you have duplicated code the block `if (val2.equals("true") || val2.equals("false"))` is the same in the next else if, second you use `boolean bool1 = Boolean.getBoolean(val1);` I think you want to use use `Boolean.parseBoolean` instead, read the java doc what `getBoolean` does, it might lead to errors. – xander Dec 08 '17 at 07:38
  • 1
    I have downvoted this question because there is far too much code here. In order to make it clear exactly where your problem is, please remove any code that is not directly causing your problem, and if you can reduce it to ten lines or less, I will consider retracting the downvote. See: [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) and [How to Debug Small Programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Joe C Dec 08 '17 at 07:45
  • @JoeC Will take note of this information for future queries. Thank you. – Mo Aboulmagd Dec 08 '17 at 08:00
  • For those who are interested, I have edited the code to reflect how I passed all of the test cases. – Mo Aboulmagd Dec 08 '17 at 09:02

1 Answers1

1

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".

xander
  • 1,780
  • 9
  • 16
  • Thank you so much. I believe that has solved part of the problem. I reformatted my code and modularized it to make it clearer as well, and revisited some of my if statements, and voila. Thanks! – Mo Aboulmagd Dec 08 '17 at 08:11