title: infix to prefix calculation
body: <p>I am trying to understand how to convert infix to prefix in order to write a method that will automatically calculate it for me. </p>
  
  <p>Here is one I have done on my own please tell me if it is correct or what I am doing wrong:</p>
  
  <blockquote>
    <p>a%b-c-(d/e)</p>
  </blockquote>
  
  <p>order of operations are:</p>
  
  <blockquote>
    <ul>
    <li>Parentheses</li>
    <li>Exponentiation  $</li>
    <li>Multiplication, division and modulus %</li>
    <li>Addition and subtraction</li>
    </ul>
  </blockquote>
  
  <p>first parenthesis: (/de)</p>
  
  <p>next  modulus:      (%ab)</p>
  
  <p>subtraction from right to left: -%ab-c/de</p>
  
  <p>last one: --%abc/de</p>
  
  <pre><code>/**
   * Calculate infix to postfix
   */
  public String calcInfixToPostfix(String eq){
  
      char arr[] = eq.toCharArray() ; 
      String val, x,y,z, w;
      boolean answer = false, notEmpty = true;
      Stack&lt;String&gt; operandStack = new Stack&lt;String&gt;();
      Stack&lt;String&gt; operatorStack = new Stack&lt;String&gt;();
  
      for(char c: arr){
  
          val = Character.toString(c);        
          answer = isOperator(val);
  
          if(!answer/* is operand */){
  
              operandStack.push(val);
  
          }
          else{
  
                  if(operatorStack.isEmpty()){
  
                      operatorStack.push(val);
                  }
                  else{
  
                      if( /* stack value */ (precedenceLevel(operatorStack.peek().charAt(0))) &gt;= (precedenceLevel(c) /* input value */) ){
  
                          do{
  
                              x = operandStack.pop();
                              y = operandStack.pop();
                              z = operatorStack.pop();
                              w = y+x+z;
                              operandStack.push(w);   
  
                              if(operatorStack.isEmpty() /* end loop if stack if empty */){                               
                              break;
                              }
                              if(/* top of operator stack */ (precedenceLevel(operatorStack.peek().charAt(0))) &lt;  (precedenceLevel(c) /* input value */) ){
                              break;
                              }
  
                          } while(notEmpty);                      
                      }
  
                      operatorStack.push(val);
                  }
          }
      }
  
      do{
          x = operandStack.pop();
          y = operandStack.pop();
          z = operatorStack.pop();
          w = y+x+z;
          operandStack.push(w);
  
          if(operatorStack.isEmpty() /* end loop if stack if empty */){                               
          break;
          }
  
      }while(notEmpty);
  
      String out = operandStack.pop();
      return out;         
  }
  
  
  /**
   * Determines if the value is an operator
   * @param val
   * @return boolean is operator or not
   */
  public boolean isOperator(String val){
  
      if(
              (val.equals(String.valueOf(add))) || (val.equals(String.valueOf(sub))) ||
              (val.equals(String.valueOf(mul))) || (val.equals(String.valueOf(div))) ||
              (val.equals(String.valueOf(mod))) || (val.equals(String.valueOf(exp))) ||
              (val.equals(String.valueOf(parL)))|| (val.equals(String.valueOf(parR)))
         )
      {           
          return true;    
      }
      else{
          return false;
      }
  }
  
  /** 
   * Calculates a value for each operator based on it's precedence
   * ORDER OF OPERATION FOR INFIX
   * Parentheses
   * Exponentiation
   * Multiplication, division and modulus
   * Addition and subtraction
   * @param op char the value being tested
   * @return int return precedence level of operator
   */
  public int precedenceLevel(char op) {
      switch (op) {
          case add:
          case sub:
              return 0;
          case mul:
          case div:
          case mod:
              return 1;
          case exp:
              return 2;
          case parL:
          case parR:
              return 3;
          default:
              System.out.println("error! invalid operator");
                  break;
      }
  }
  </code></pre>