File tree 2 files changed +13
-0
lines changed
main/java/com/thealgorithms/others
test/java/com/thealgorithms/others 2 files changed +13
-0
lines changed Original file line number Diff line number Diff line change @@ -16,6 +16,9 @@ public static int postfixEvaluate(final String exp) {
16
16
if (tokens .hasNextInt ()) {
17
17
s .push (tokens .nextInt ()); // If int then push to stack
18
18
} else { // else pop top two values and perform the operation
19
+ if (s .size () < 2 ) {
20
+ throw new IllegalArgumentException ("exp is not a proper postfix expression (too few arguments)." );
21
+ }
19
22
int num2 = s .pop ();
20
23
int num1 = s .pop ();
21
24
String op = tokens .next ();
Original file line number Diff line number Diff line change @@ -30,4 +30,14 @@ public void testIfEvaluateThrowsExceptionForInproperInput() {
30
30
public void testIfEvaluateThrowsExceptionForInputWithUnknownOperation () {
31
31
assertThrows (IllegalArgumentException .class , () -> StackPostfixNotation .postfixEvaluate ("3 3 !" ));
32
32
}
33
+
34
+ @ Test
35
+ public void testIfEvaluateThrowsExceptionForInputWithTooFewArgsA () {
36
+ assertThrows (IllegalArgumentException .class , () -> StackPostfixNotation .postfixEvaluate ("+" ));
37
+ }
38
+
39
+ @ Test
40
+ public void testIfEvaluateThrowsExceptionForInputWithTooFewArgsB () {
41
+ assertThrows (IllegalArgumentException .class , () -> StackPostfixNotation .postfixEvaluate ("2 +" ));
42
+ }
33
43
}
You can’t perform that action at this time.
0 commit comments