Click here to Skip to main content
15,610,088 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Java
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());
   }
}


I Keep Getting this:
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.


What I have tried:

I just cant figure anything out.
Posted
Updated 29-Jan-18 11:29am
v2

You should put a breakpoint at the beginning of your mathTheStack method and launch a debug session; this will allow you to check the three stacks and to understand why the line 32 of the BiggerStackProject.java file causes the exception to be thrown.
Quickly checking your algorithm, I think this part may be of interest:
Java
for(int x = 0; x < input.size(); x++){
         if(input.peek() == "*" || input.peek() == "/" || input.peek() == "%"){
            operators.push(input.pop());
         }
         else{
            if(operators.pop().equals("*")){
               // ...

On the first iteration (x = 0), if the first character is a digit, the else block is executed. At this point, you did not push anything into the operators stack yet, and you try to pop from it, which throws the exception.
There are several ways to do what you are trying to achieve, but one of the first things you could try is to qualify both numbers and operators stacks first, before trying to use them. You only qualified the numbers stack in your first loop, so maybe you could rework this loop to qualify both stacks?

As a side note, why do you handle numbers as strings? Stack class is generic, meaning you can use a Stack<double> as well.
 
Share this answer
 
v2
Comments
Member 13650475 30-Jan-18 23:02pm    
yeah Ima try that. also I am handling them as strings because this assignment specifically says to use Stacks of strings.
I feel likeyour ysage if operators.pop() is wrong in your code:
Java
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())));
}

You need to understand what you do with the stack, the debugger is the only tool that can show you wgat your code is doing with the stack.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900