Repository: TheAlgorithms/Java
Pull request #4261 (2023-07-25 18:14:09)

fix: handle incorrect inputs in `StackPostfixNotation`
The current implementation of [`StackPostfixNotation.postfixEvaluate`](https://github.com/TheAlgorithms/Java/blob/ef4ef42ed3a843ea55eb4ac90afc3574877fe417/src/main/java/com/thealgorithms/others/StackPostfixNotation.java#L15):
- crashed for empty input (i.e. `""`),
- returns 3 for the input `"3 3 3"`.

This PR:
- adds missing tests,
- does some basic linting,
- and fixes the above issues.

I did not update the doc string, since I believe this class needs some refactoring anyway (e.g. the method `postfixEvaluate` is way too long). I am also concerned with the current formatting: [this line](https://github.com/TheAlgorithms/Java/blob/98b1d085fdefb6f087f3e52c866bf46de2eb578d/src/test/java/com/thealgorithms/others/StackPostfixNotationTest.java#L13) seems way too long. In my opinion both of these are out of scope for this PR.

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.



Patch:
diff --git a/src/main/java/com/thealgorithms/others/StackPostfixNotation.java b/src/main/java/com/thealgorithms/others/StackPostfixNotation.java
index 08b0f9f4..c6d395cb 100644
--- a/src/main/java/com/thealgorithms/others/StackPostfixNotation.java
+++ b/src/main/java/com/thealgorithms/others/StackPostfixNotation.java
@@ -1,18 +1,14 @@
 package com.thealgorithms.others;
 
-import java.util.*;
+import java.util.Scanner;
+import java.util.Stack;
 
-public class StackPostfixNotation {
-
-    public static void main(String[] args) {
-        Scanner scanner = new Scanner(System.in);
-        String post = scanner.nextLine(); // Takes input with spaces in between eg. "1 21 +"
-        System.out.println(postfixEvaluate(post));
-        scanner.close();
+public final class StackPostfixNotation {
+    private StackPostfixNotation() {
     }
 
     // Evaluates the given postfix expression string and returns the result.
-    public static int postfixEvaluate(String exp) {
+    public static int postfixEvaluate(final String exp) {
         Stack<Integer> s = new Stack<Integer>();
         Scanner tokens = new Scanner(exp);
 
@@ -28,12 +24,16 @@ public class StackPostfixNotation {
                     case "+" -> s.push(num1 + num2);
                     case "-" -> s.push(num1 - num2);
                     case "*" -> s.push(num1 * num2);
-                    default -> s.push(num1 / num2);
+                    case "/" -> s.push(num1 / num2);
+                    default -> throw new IllegalArgumentException("exp contains an unknown operation.");
                 }
                 //  "+", "-", "*", "/"
             }
         }
         tokens.close();
+        if (s.size() != 1) {
+            throw new IllegalArgumentException("exp is not a proper postfix expression.");
+        }
         return s.pop();
     }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/others/StackPostfixNotationTest.java b/src/test/java/com/thealgorithms/others/StackPostfixNotationTest.java
new file mode 100644
index 00000000..4894b403
--- /dev/null
+++ b/src/test/java/com/thealgorithms/others/StackPostfixNotationTest.java
@@ -0,0 +1,33 @@
+package com.thealgorithms.others;
+
+import static java.util.Map.entry;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import java.util.Map;
+import org.junit.jupiter.api.Test;
+
+public class StackPostfixNotationTest {
+    @Test
+    public void testEvaluate() {
+        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));
+        for (final var tc : testCases.entrySet()) {
+            assertEquals(tc.getValue(), StackPostfixNotation.postfixEvaluate(tc.getKey()));
+        }
+    }
+
+    @Test
+    public void testIfEvaluateThrowsExceptionForEmptyInput() {
+        assertThrows(IllegalArgumentException.class, () -> StackPostfixNotation.postfixEvaluate(""));
+    }
+
+    @Test
+    public void testIfEvaluateThrowsExceptionForInproperInput() {
+        assertThrows(IllegalArgumentException.class, () -> StackPostfixNotation.postfixEvaluate("3 3 3"));
+    }
+
+    @Test
+    public void testIfEvaluateThrowsExceptionForInputWithUnknownOperation() {
+        assertThrows(IllegalArgumentException.class, () -> StackPostfixNotation.postfixEvaluate("3 3 !"));
+    }
+}