mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-10 21:43:15 +08:00
Handle inputs like "2 +"
in StackPostfixNotation
(#4262)
This commit is contained in:
@ -16,6 +16,9 @@ public final class StackPostfixNotation {
|
||||
if (tokens.hasNextInt()) {
|
||||
s.push(tokens.nextInt()); // If int then push to stack
|
||||
} else { // else pop top two values and perform the operation
|
||||
if (s.size() < 2) {
|
||||
throw new IllegalArgumentException("exp is not a proper postfix expression (too few arguments).");
|
||||
}
|
||||
int num2 = s.pop();
|
||||
int num1 = s.pop();
|
||||
String op = tokens.next();
|
||||
|
Reference in New Issue
Block a user