Note that there are some explanatory texts on larger screens.

plurals
  1. POStack problem java. Postfix Evaluation
    text
    copied!<p>Hey Guys I'm having a problem when I run my program. In the PostfixEvaluate() Method is where it takes in a string and solves the postfix problem and returns it. Well when I go to run it, I'm getting a bunch of random numbers(some repeated), I'm going crazy because I don't know what else to try and I've spent more time on this than it should normally take. </p> <p>Heres the PostfixEvaluate Method:</p> <pre><code> public int PostfixEvaluate(String e){ //String Operator = ""; int number1; int number2; int result=0; char c; //number1 = 0; //number2 = 0; for(int j = 0; j &lt; e.length(); j++){ c = e.charAt(j); if (c != '+'&amp;&amp; c!= '*' &amp;&amp; c!= '-' &amp;&amp; c!= '/') { //if (c == Integer.parseInt(e)) { s.push(c); } else { number1 = s.pop(); number2 = s.pop(); switch(c) { case '+': result = number1 + number2; break; case '-': result = number1 - number2; break; case '*': result = number1 * number2; break; case '/': result = number1 / number2; break; } s.push(result); } System.out.println(result); } return s.pop(); } public static void main(String[] args) { Stacked st = new Stacked(100); String y = new String("(z * j)/(b * 8) ^2"); String x = new String("2 3 + 9 *"); TestingClass clas = new TestingClass(st); clas.test(y); clas.PostfixEvaluate(x); } } </code></pre> <p>This is the Stack Class: </p> <pre><code> public class Stacked { int top; char stack[]; int maxLen; public Stacked(int max) { top = -1; maxLen = max; stack = new char[maxLen]; } public void push(int result) { top++; stack[top] = (char)result; } public int pop() { int x; x = stack[top]; //top = top - 1; top--; return x; } public boolean isStackEmpty() { if(top == -1) { System.out.println("Stack is empty " + "Equation Good"); return true; } else System.out.println("Equation is No good"); return false; } public void reset() { top = -1; } public void showStack() { System.out.println(" "); System.out.println("Stack Contents..."); for(int j = top; j &gt; -1; j--){ System.out.println(stack[j]); } System.out.println(" "); } public void showStack0toTop() { System.out.println(" "); System.out.println("Stack Contents..."); for(int j=0; j&gt;=top; j++){ System.out.println(stack[j]); } System.out.println(" "); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload