title: Infix to postfix calculator produces prefix and sometimes keeps on running
body: <p>I'm trying to code an infix to postfix calculator. So far what I have got after inserting infix expression is either a prefix expression or the code keeps on running and does nothing. Here is what I've made:</p>
  
  <pre><code>import java.util.Scanner;
  import java.util.Stack;
  
  public class postFix {
  
      public static StringBuilder postFixExp = new StringBuilder();
  
      public static int priority(char ch){
          if(ch == '^') return 3;
          if(ch == '/' || ch == '*') return 2;
          if(ch == '+' || ch == '-') return 1;
          return 0;
          }
  
  
      public static String postfix(){ 
          Stack&lt;Character&gt; st = new Stack&lt;Character&gt;();
          Scanner scan = new Scanner(System.in);
          System.out.println("Sisestage infiks: ");
          String sone = scan.next();
          scan.close();
  
          for(int i = 0; i&lt;sone.length(); i++){
              char ch = sone.charAt(i);
  
              switch(ch){
              case'+': 
              case'-': 
              case'*': 
              case'/': 
                  postFixExp.append(' ');
                  st.push(ch);
                  break;
  
          //  case' ':
              case'(':    
                  st.push(ch);
                  break;
  
              case')': 
                  while(st.peek() != '('){
                      postFixExp = postFixExp.append(st.pop());                   
                  }
                  st.pop();
                  break;
              default:
                  while(!st.isEmpty() &amp;&amp; st.peek() != '(' &amp;&amp; priority(ch) &lt;= priority(st.peek()))
                  {
                      postFixExp = postFixExp.append(st.pop());
                  }
                  st.push(ch);
                  break;          
              }
          }
          while(!st.isEmpty()){
              postFixExp = postFixExp.append(st.pop());
          }
          return postFixExp.toString();
      }
  
      public static void main(String[] args) {
          System.out.println(postfix());
      }
  }
  </code></pre>
  
  <p>I used <a href="http://andromeda.rutgers.edu/~loftin/datafal06/" rel="nofollow">this pseudocode</a>.</p>
  
  <p>What's wrong with my code?</p>
  
  <p>If I input :</p>
  
  <pre><code>1+2 
  </code></pre>
  
  <p>It returns </p>
  
  <pre><code>+12
  </code></pre>
  
  <p>I would like to see </p>
  
  <pre><code>1 2 + or 12+
  </code></pre>