Исключение пустого стека!
import java.util.*; public class BiggerStackProject { public static void main(String args[]){ String tester = "5+8-6*8"; Stack<string> test = stackTheString(tester); int size = test.size(); System.out.println(mathTheStack(test)); } public static Stack<string> stackTheString(String input){ Stack<string> original = new Stack<string>(); String superTemp = null; for(int x = 0; x < input.length(); x++){ String temp = Character.toString(input.charAt(x)); original.push(temp); } return original; } public static double mathTheStack(Stack<string> input){ Stack<string> operators = new Stack<string>(); Stack<string> numbers = new Stack<string>(); for(int x = 0; x < input.size(); x++){ if(!(input.peek() == "+" || input.peek() == "-" || input.peek() == "*" || input.peek() == "/" || input.peek() == "%")){ numbers.push(input.pop()); } } for(int x = 0; x < input.size(); x++){ if(input.peek() == "*" || input.peek() == "/" || input.peek() == "%"){ operators.push(input.pop()); } else{ if(operators.pop().equals("*")){ numbers.push(String.valueOf(Double.parseDouble(numbers.pop()) * Double.parseDouble(numbers.pop()))); } else if(operators.pop().equals("/")){ numbers.push(String.valueOf(Double.parseDouble(numbers.pop()) / Double.parseDouble(numbers.pop()))); } else if(operators.pop().equals("+")){ numbers.push(String.valueOf(Double.parseDouble(numbers.pop()) + Double.parseDouble(numbers.pop()))); } else if(operators.pop().equals("-")){ numbers.push(String.valueOf(Double.parseDouble(numbers.pop()) - Double.parseDouble(numbers.pop()))); } else if(operators.pop().equals("%")){ numbers.push(String.valueOf(Double.parseDouble(numbers.pop()) % Double.parseDouble(numbers.pop()))); } } } return Integer.parseInt(numbers.pop()); } }
Я продолжаю получать это:
Exception in thread "main" java.util.EmptyStackException at java.util.Stack.peek(Stack.java:85) at java.util.Stack.pop(Stack.java:67) at BiggerStackProject.mathTheStack(BiggerStackProject.java:32) at BiggerStackProject.main(BiggerStackProject.java:8) Process completed.
Что я уже пробовал:
Я просто ничего не могу понять.