title: Calculate negatives in the stack calculator
body: <p>Bear with me, I'm a little fried right now from getting this all together, but I'm at the final stretch. I've made a calculator in java that takes an infix equation and then changes it to postfix. It also takes variables! I'm made it so my postfix includes negative numbers from infix. This would look like so:</p>
  
  <pre><code>infix: 1+-2*(4/2)
  postfix: 12_42/*+
  </code></pre>
  
  <p>So obviously I got it working where negatives are <code>'_'</code> in postfix. Cool yeah? Ok, but now I gotta get my calculator to read them and I'm just getting brain-fart on where it goes on the stack and what I do to make it work without making a condition for all forms of operands. Here is what I have:</p>
  
  <pre><code>import java.util.Stack;
  
  /**
   *
   * @author rtibbetts268
   */
  public class InfixToPostfix
  {
          /**
           * Operators in reverse order of precedence.
           */
      private static final String operators = "-+/*_";
      private static final String operands = "0123456789x";
  
      /*public int evalInfix(String infix)
      {
          return evaluatePostfix(convert2Postfix(infix));
      }*/
  
      public String xToValue(String postfixExpr, String x)
      {
          char[] chars = postfixExpr.toCharArray();
          StringBuilder newPostfixExpr = new StringBuilder();
  
          for (char c : chars)
          {
              if (c == 'x')
              {
                  newPostfixExpr.append(x);
              }
              else
              {
                  newPostfixExpr.append(c);
              }
          }
          return newPostfixExpr.toString();
      }
  
      public String convert2Postfix(String infixExpr)
      {
          char[] chars = infixExpr.toCharArray();
          StringBuilder in = new StringBuilder(infixExpr.length());
  
          for (int i = 0; i&lt;chars.length; i++)
          {
              if (infixExpr.charAt(i) == '-')
              {
                  if (i == 0)
                  {
                  in.append('_');
                  }
                  else if(isOperand(infixExpr.charAt(i + 1)))
                  {
                      if (i != infixExpr.length())
                      {
                      if (isOperator(infixExpr.charAt(i-1)))
                          in.append('_');
                      }
                      else
                      {
                          in.append(infixExpr.charAt(i));
                      }
                  }
                  else
                  {
                     in.append(infixExpr.charAt(i));
                  }
              }
              else
              {
                  in.append(infixExpr.charAt(i));
              }
          }
  
          chars = in.toString().toCharArray();
          Stack&lt;Character&gt; stack = new Stack&lt;Character&gt;();
          StringBuilder out = new StringBuilder(in.toString().length());
  
          for (char c : chars)
          {
              if (isOperator(c))
              {
                  while (!stack.isEmpty() &amp;&amp; stack.peek() != '(')
                  {
                      if (operatorGreaterOrEqual(stack.peek(), c))
                      {
                          out.append(stack.pop());
                      }
                      else
                      {
                          break;
                      }
                  }
                  stack.push(c);
              }
              else if (c == '(')
              {
                  stack.push(c);
              }
              else if (c == ')')
              {
                  while (!stack.isEmpty() &amp;&amp; stack.peek() != '(')
                  {
                      out.append(stack.pop());
                  }
                  if (!stack.isEmpty())
                  {
                      stack.pop();
                  }
              }
              else if (isOperand(c))
              {
                  out.append(c);
              }
          }
          while (!stack.empty())
          {
              out.append(stack.pop());
          }
          return out.toString();
      }
  
      public int evaluatePostfix(String postfixExpr)//YBEYFCNUNKJKDV IT'S RIGHT HERE!!!
      {
          char[] chars = postfixExpr.toCharArray();
          Stack&lt;Integer&gt; stack = new Stack&lt;Integer&gt;();
          for (char c : chars)
          {
              if (isOperand(c))
              {
                  stack.push(c - '0'); // convert char to int val
              }
              else if (isOperator(c))
              {
                  int op1 = stack.pop();
                  int op2 = stack.pop();
                  int result;
                  switch (c) {
                      case '_':
                          result = op1 * -1;
                          //stack.push(result);
                          //break;
                      case '*':
                          result = op1 * op2;
                          stack.push(result);
                          break;
                      case '/':
                          result = op2 / op1;
                          stack.push(result);
                          break;
                      case '+':
                          result = op1 + op2;
                          stack.push(result);
                          break;
                      case '-':
                          result = op2 - op1;
                          stack.push(result);
                          break;
                  }
              }
          }
          return stack.pop();
      }
  
      private int getPrecedence(char operator)
      {
          int ret = 0;
          if (operator == '-' || operator == '+')
          {
              ret = 1;
          }
          else if (operator == '*' || operator == '/')
          {
              ret = 2;
          }
          if (operator == '_')
          {
              ret = 3;
          }
          return ret;
      }
  
      private boolean operatorGreaterOrEqual(char op1, char op2)
      {
          return getPrecedence(op1) &gt;= getPrecedence(op2);
      }
  
      private boolean isOperator(char val)
      {
          return operators.indexOf(val) &gt;= 0;
      }
  
      private boolean isOperand(char val)
      {
          return operands.indexOf(val) &gt;= 0;
      }
  
  }
  </code></pre>
  
  <p>I would post less, but it all tends to work cohesively so I'll explain. Look closely at the method called <code>evaluatePostfix()</code> (it's the third one from the top).</p>
  
  <p>This is where the class can take a postfix expression such as the one I named above and calculates it. It calculates an expression with all positive integers, but when I tried to implement it to change a number to negative before it actually ran any of the math, it goes kaputt.</p>
  
  <p>Could anyone assist me with fixing this specific method so it works? I need it to calculate negative integers as well of positive.</p>