Skip to content

Commit 44dcebb

Browse files
authored
Handle incorrect inputs in StackPostfixNotation (TheAlgorithms#4261)
1 parent dec3b98 commit 44dcebb

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
package com.thealgorithms.others;
22

3-
import java.util.*;
3+
import java.util.Scanner;
4+
import java.util.Stack;
45

5-
public class StackPostfixNotation {
6-
7-
public static void main(String[] args) {
8-
Scanner scanner = new Scanner(System.in);
9-
String post = scanner.nextLine(); // Takes input with spaces in between eg. "1 21 +"
10-
System.out.println(postfixEvaluate(post));
11-
scanner.close();
6+
public final class StackPostfixNotation {
7+
private StackPostfixNotation() {
128
}
139

1410
// Evaluates the given postfix expression string and returns the result.
15-
public static int postfixEvaluate(String exp) {
11+
public static int postfixEvaluate(final String exp) {
1612
Stack<Integer> s = new Stack<Integer>();
1713
Scanner tokens = new Scanner(exp);
1814

@@ -28,12 +24,16 @@ public static int postfixEvaluate(String exp) {
2824
case "+" -> s.push(num1 + num2);
2925
case "-" -> s.push(num1 - num2);
3026
case "*" -> s.push(num1 * num2);
31-
default -> s.push(num1 / num2);
27+
case "/" -> s.push(num1 / num2);
28+
default -> throw new IllegalArgumentException("exp contains an unknown operation.");
3229
}
3330
// "+", "-", "*", "/"
3431
}
3532
}
3633
tokens.close();
34+
if (s.size() != 1) {
35+
throw new IllegalArgumentException("exp is not a proper postfix expression.");
36+
}
3737
return s.pop();
3838
}
3939
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.thealgorithms.others;
2+
3+
import static java.util.Map.entry;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
import static org.junit.jupiter.api.Assertions.assertThrows;
6+
7+
import java.util.Map;
8+
import org.junit.jupiter.api.Test;
9+
10+
public class StackPostfixNotationTest {
11+
@Test
12+
public void testEvaluate() {
13+
final Map<String, Integer> testCases = Map.ofEntries(entry("1 1 +", 2), entry("2 3 *", 6), entry("6 2 /", 3), entry("-5 -2 -", -3), entry("5 2 + 3 *", 21), entry("-5", -5));
14+
for (final var tc : testCases.entrySet()) {
15+
assertEquals(tc.getValue(), StackPostfixNotation.postfixEvaluate(tc.getKey()));
16+
}
17+
}
18+
19+
@Test
20+
public void testIfEvaluateThrowsExceptionForEmptyInput() {
21+
assertThrows(IllegalArgumentException.class, () -> StackPostfixNotation.postfixEvaluate(""));
22+
}
23+
24+
@Test
25+
public void testIfEvaluateThrowsExceptionForInproperInput() {
26+
assertThrows(IllegalArgumentException.class, () -> StackPostfixNotation.postfixEvaluate("3 3 3"));
27+
}
28+
29+
@Test
30+
public void testIfEvaluateThrowsExceptionForInputWithUnknownOperation() {
31+
assertThrows(IllegalArgumentException.class, () -> StackPostfixNotation.postfixEvaluate("3 3 !"));
32+
}
33+
}

0 commit comments

Comments
 (0)