I've been coding a class that takes a string input and then converts it into postfix notation. For some reason I cannot seem to find a bug that I have. Everything seems to work ok until the couple of last test cases that I run.
My input test case: (a*(b+c)-(d-e)/f) *g/(h-i)-j *k
My expected output test case: abc+*de-f/-g*hi-/jk*-
My output case: abc+de-f/-*ghi-jk*-/*
I've been looking at my code for like 2 hrs trying different things to fix the problem, but I still can't seem to find what the problem is. If someone could help me with identifying the problem, I would greatly appreciate it.
Here's the class:
import java.util.Stack;
public class InfixToPostfixConverter
{
//**********************************************************************
//The precedence method determines the precedence between two operators.
//If the first operator is of higher or equal precedence than the second
//operator, it returns the value true, otherwise it returns false.
//***********************************************************************
public static boolean precedence(char topStack, char currentChar)
{
   if(topStack == '*' || topStack == '/'){
       if(currentChar == '+' || currentChar == '-'){
           return true;
       }
   }
   else if(topStack == currentChar){
       return true;
   }
   else if(topStack == '+' && currentChar == '-'){
      return true;
   }
   else if(topStack == '-' && currentChar == '+'){
      return true;
   }
   else if(topStack == '*' && currentChar == '/'){
       return true;
   }
   else if(topStack == '/' && currentChar == '*'){
       return true;
   }
   return false;
}
char currentChar = infixString.charAt(i);
    //Case A:
     if(currentChar != '+' && currentChar != '-' && currentChar != '*' && currentChar != '/' && currentChar != '(' && currentChar != ')' ){
         postfixString += currentChar;
     }
    //Case B:
     else if(currentChar == '('){
         stack1.push(currentChar);
     }
    //Case C:
     else if(stack1.isEmpty() && currentChar == '+' || currentChar == '-' || currentChar == '*' || currentChar == '/'){
         stack1.push(currentChar);
     }
    //Case D:
     else if(currentChar == '+' || currentChar == '-' || currentChar == '*' || currentChar == '/' && !stack1.isEmpty()){
         char topStack = stack1.peek();
        while(!stack1.isEmpty()){
            if(precedence(topStack, currentChar)){
                postfixString += stack1.pop();
            }
            else{
                stack1.push(currentChar);
                break;
            } 
        }
     }
    //Case E:
     else if(currentChar == ')' && !stack1.isEmpty()){
         while(!stack1.isEmpty() && stack1.peek() != '('){
             postfixString += stack1.pop();
         }
         stack1.pop();
     }
  } //end of for loop
    //Case F:
  if(!stack1.isEmpty() && stack1.peek() == '('){
      return "No matching close parenthesis error.";
  }
  else if(!stack1.isEmpty() && stack1.peek() != '('){
      while(!stack1.isEmpty() && stack1.peek() != '('){
          postfixString += stack1.pop();
      }
  }
  return postfixString;
}//end of convertToPostfix method
}//end of the InfixToPostfixConverter class