Help! I don't understand InFix to PostFix conversion.

Alright so this is the code I have so far:

public static void main(String[] args) {

String inFix = "2*2+2";

String postFix = InfixToPostfix(inFix);

System.out.println("InFix: " + inFix);

System.out.println("PostFix: " + postFix);

}

public static String InfixToPostfix(String equation) {

int priority = 0;

String result = "";

char temp = equation.charAt(0);

Deque<String> stack = new ArrayDeque<>();

stack.push(temp + "");

for (int i = 1; i < equation.length(); i++) {

temp = equation.charAt(i);

if (Character.isDigit(temp)) {

result += temp;

}

else {

Character operator = stack.peek().charAt(0);

if (operator == '*' || operator == '/')

priority = 1;

else

priority = 0;

if (priority == 1) {

result += stack.pop();

i--;

}

else {

if (temp == '+' || temp == '-') {

result += stack.pop();

stack.push(temp + "");

}

else

stack.push(temp + "");

}

}

}

int length = stack.size();

for (int i = 0; i < length; i++) {

result += stack.pop();

}

return result;

}

This seems to work fine when I DON'T multiply or divide the first set of numbers (ex: 2+22) BUT when I try to divide / multiply the first set (ex: 22+2) everything messes up.

If I put in 22+2 the result ends up being: 222+. BUT If I put in 2+22 the result is 222+. The result should be 222*+ for both cases. What am I doing wrong!?

Re: Help! I don't understand InFix to PostFix conversion.

Please see the announcements page for the use of code tags when posting code on the forum.

Go through the code one step at a time to see how the result is what it is. Only after you understand what it is doing can you begin to correct it.