   [java] Infix To PostFix conversion
  Hi all,Im trying to do a function that converts an infix expression to and postfix expression using Stacks.I have done this:public class Formula {    public Boolean ValidarFormula(String formula)    {   Stack pila;   pila = new Stack<String>();   for(int i=0; i < formula.length(); i++)   {  String c = String.valueOf(formula.charAt(i));  if(EsSimboloIzquierda(c))  { pila.push(c);  }  else if(EsSimboloDerecha(c))  { String s = (String)pila.pop();if( (s!=null && c.equals(")") && s.equals("("))==false ){    pila.push(c);}  }   }   if(pila.empty())  return true;   else  return false;    }    private Boolean EsSimboloDerecha(String s)    {   if(s.equals("}") || s.equals("]") || s.equals(")"))   {  return true;   }   else  return false;    }    private Boolean EsSimboloIzquierda(String s)    {   if(s.equals("{") || s.equals("[") || s.equals("("))   {  return true;   }   else  return false;    }    private Boolean EsSimboloPermitido(String s)    {   if(s.equals("{") || s.equals("}") || s.equals("[") || s.equals("]") || s.equals("(") || s.equals(")"))   {  return true;   }   else  return false;    }    private Boolean EsOperador(String s)    {   if(s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/") || s.equals("^"))  return true;   return false;    }    private int pred_level(char ch)    {   if ( ch == '+' || ch == '-' )  return 1;   else if ( ch == '^' )  return 3;   else  return 2;    }    public int precedencia(char operador1, char operador2)    {   if ( pred_level(operador1) > pred_level(operador2) )  return 1;   else if ( pred_level(operador1) < pred_level(operador2) )  return -1; else  return 0;    }    public Boolean EsDigito(String s)    {   try   {  Integer.parseInt(s);   }   catch(NumberFormatException nfe)   {  return false;   }   return true;    }   public LinkedList<String> InfixToPostfix(LinkedList<String> infija)    {   LinkedList<String> postfija = new LinkedList<String>();   Stack<String> pila = new Stack<String>();   try   {  for(String ch : infija)  { if(EsDigito(ch)) {postfija.addLast(ch); } else if(ch.equals("(")) {pila.push(ch); } else if(ch.equals(")")) {String temp;do{    temp = pila.pop();    postfija.add(temp);}while(temp.equals("(")); } else {String top = pila.peek();//System.out.println(top);while(pila.empty()==false && top.equals("(")==false && precedencia(ch.charAt(0), top.charAt(0))>=0){    pila.pop();    postfija.add(top);}pila.push(ch); }  }  return postfija;   }   catch(Exception ex)   {  System.out.println();  for(String s: postfija) System.out.print(s);    return null;   }    }}public static void main(String [] args){   try   {  String formula = "((1+2)/(3-SUM(4–5)*PROM(6-(7^3))))/8";  Formula f = new Formula();//System.out.println(f.ValidarFormula(formula));  LinkedList<String> infija = new LinkedList<String>();  infija.add("(");  infija.add("1");  infija.add("+");  infija.add("2");  infija.add(")");  infija.add("*");  infija.add("2");  System.out.println();  for(String s : infija)  { System.out.print(s);  }  /*  System.out.println("asd");  for(int i=0;i<formula.length();i++)  { String n = formula.substring(i, i); System.out.println(n); infija.add(n);  }   **/  /*System.out.println(f.precedencia('+', '-'));  System.out.println(f.precedencia('+', '*'));  System.out.println(f.precedencia('+', '-'));  System.out.println(f.precedencia('*', '/'));*/  LinkedList<String> postfija = f.InfixToPostfix(infija);  if(postfija==null) System.out.println("postfija is null");  for(String s : postfija)  { System.out.println(s);  }/*  for(int i=0;i<postfija.size();i++)  { System.out.print(postfija.get(i));  }*/   }   catch(Exception ex)   {}    }but I've got some errorsAny idea?Im using this: http://personal.denison.edu/~havill/algorithmics/algs/postfix.pdfin order to construct my algorithmThanks,MaxDemian   
  1) the paper you are using is wrong. The Stack has to be emptied into the postfix expression as the last step.wikipedia2) right parenthesis loop condition has the inverse logic and it hasn't been properly interpreted. The paper you are using doesn't help either.3) operator loop condition checks for empty stack, however you are getting the top element before the loop, that's wrong because the stack could be empty e.g: 1+2code should be something like this:public List<String> infixToPostfix(List<String> infija) throws Exception {List<String> postfija = new ArrayList<String>();Stack<String> pila = new Stack<String>();for (String token : infija) {if (isDigito(token)) {postfija.add(token);} else if("(".equals(token)) {pila.push(token);} else if(")".equals(token)) {String temp;do {temp = pila.pop();System.out.println("# "  + temp);if("(".equals(temp)) {break;}postfija.add(temp);} while(!"(".equals(temp));} else {while(!pila.empty() && !"(".equals(pila.peek()) && precedencia(token, pila.peek()) >= 0) {String pop = pila.pop();postfija.add(pop);}pila.push(token);}}while(!pila.empty()) {postfija.add(pila.pop());}return postfija;}¡ Hala, suerte con el código, y a ver si lo terminas bien !;  Thanks :) I have solved this out.   