Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You need to split the string into tokens first:</p> <pre><code>/* Splits the expression up into several Strings, * all of which are either a number or and operator, * none of which have spaces in them. */ String [] expressionAsTokens = e.split(" "); </code></pre> <p>Then you need to make sure you compare Strings, not chars:</p> <pre><code>//compare strings instead of chars String token = expressionAsTokens[j]; if (!"+".equals(token) &amp;&amp; !"*".equals(token) &amp;&amp; !"-".equals(token) &amp;&amp; !"/".equals(token)) { s.push(Integer.parseInt(token)); } else { //same code as you had before } </code></pre> <p>Also, is there any reason you are storing everything as a <code>char</code> array in your <code>Stacked</code> class? Your <code>pop()</code> method returns and integer, yet everything is stored as a <code>char</code>.</p> <p>For this application, everything should be stored as an integer:</p> <pre><code>public class Stacked { int stack[]; // array is of type integer int top; int maxLen; // constructor public void push() {/*...*/} public int pop() {/*...*/} //pop returns an int as before //... } </code></pre> <p>One final note: Be careful what order you add and subtract the numbers in. I don't remember if postfix operands are evaluated left first or right first, but make sure you get them in the right order. As you have it now, <code>2 3 - 4 *</code> would evaluate as <code>4 * (3 - 2)</code> and I think it should be <code>(2 - 3) * 4</code>. This won't matter with adding and multiplying, but it will with subtracting and dividing.</p>
 

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