title: Infix to Postfix Java Algorithm Issue
body: <p>So I was assigned to create an method which takes in a string in infix notation as a parameter and returns a string in postfix notation.</p>
  
  <p>My code seems to work for most examples I throw at it, but a few inputs cause wacky results.</p>
  
  <pre><code>public class Operations&lt;T&gt; {
  
  public int value(char c){
      switch(c){
      case '(':
      case ')':
          return 3;
      case '*':
      case '/':
      case '%':
          return 2;
      case '+':
      case '-':
          return 1;
      default:
          return 0;   
      }
  }
  
  public String infixToPostfix(String infix){
  
      //Operator stack
      myStack&lt;Character&gt; ops = new myStack&lt;Character&gt;();
  
      //Postfix string
      String postfix = "";
  
      //Current char being read
      char c;
  
      //Marks if paranthesis are being passed in
      boolean flag = false;
  
      //Iterate through each character to find operators
      for(int i=0; i&lt;infix.length(); i++){
          c = infix.charAt(i);
  
          //Add operand to postfix and operator to stack
          if(value(c)==0){
              postfix+=c;
          } else if(ops.isEmpty() || (value(c)&gt;value(ops.getTop()) &amp;&amp; c!=')') || c=='(') {
              ops.push(c);
          } else if(value(c)&lt;value(ops.getTop()) &amp;&amp; c!=')') {
              if(ops.getTop()=='(' || flag) {
                  ops.push(c);
                  flag = true;
              } else {
                  postfix+=ops.pop();
                  while(!ops.isEmpty() &amp;&amp; value(c)&lt;value(ops.getTop())) {
                      postfix+=ops.pop();
                  }
                  ops.push(c);
              }
          } else if(c==')') { 
              while(ops.getTop()!='('){
                  postfix+=ops.pop();
              }
              ops.pop();
              flag = false;
          } else {
              postfix+=ops.pop();
              ops.push(c);
          }
      }
      while(!ops.isEmpty()){
          postfix+=ops.pop();
      }       
  
      return postfix;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>for example, the equation:
  - A * B + (C/D*7) – ( (A%C-8) / (H+F-D))</p>
  
  <p>outputs:</p>
  
  <blockquote>
    <p>AB<em>CD/7</em>+AC8-%HF+D-/-</p>
  </blockquote>
  
  <p>while the correct answer is:</p>
  
  <blockquote>
    <p>AB<em>CD/7</em>+AC%8-HF+D-/</p>
  </blockquote>
  
  <p>What is causing the problem? Thanks</p>