stack - Parenthesis error check for infix to postfix conversion Java -


i have been working on program infix postfix conversion , have working except cannot figure out put error check missing left parenthesis. user inputs string , program goes class , converts want make sure have entered correct amount of parenthesis. have tried multiple places keep coming emptystackexceptions.

import java.util.*;  public class postfixconversion {      public static boolean precedence(char first, char second)    {       int v1 = 0, v2 = 0;       //find value first       if(first == '-' || first == '+'){          v1 = 1;       }else if(first == '*' || first == '/'){          v1 = 2;           }//end if        //find value second       if(second == '-' || second == '+'){          v2 = 1;       }else if(second == '*' || second == '/'){          v2 = 2;           }//end if       if(v1 < v2){         return false;      }//end if       return true;   }//end precedence method   //converts infix expression postfix expression  public static string converttopostfix(string infixexp)   {      string postfix = "the postfix expression is: ";      stack<character> stack = new stack<character>();      char character = ' ';       for(int = 0; < infixexp.length(); i++)      {          character = infixexp.charat(i);           //determine if character operator          if(character == '*' || character == '-' || character == '/' || character == '+')          {              while(!stack.empty() && precedence(stack.peek(), character)){                  postfix += stack.pop();              }//end while              stack.push(character);          }          else if(character == '(') //check left parenthesis          {              stack.push(character);          }          else if (character == ')')           {              while(!stack.peek().equals('(') && !stack.isempty()){ //add characters until left parenthesis                  postfix += stack.pop();              }//end while               if(!stack.isempty() && stack.peek().equals('(')){                  stack.pop(); // pop/remove left parenthesis              }          }          else          {              postfix += character;          }//end if      }//end      while(!stack.empty()) //add remaining elements of stack postfix expression      {          if(stack.peek().equals('('))          {              postfix = "there no matching right parenthesis.";              return postfix;          }          postfix += stack.pop();      }          return postfix;  }//end converttopostfix } 

first of all, have change while loop form:

while (!stack.empty() && precedence(stack.peek(), character)) {     postfix += stack.pop(); } 

i.e. change order of expressions in while's check: stack.empty() check should first.

second fix addition of "there no matching left parenthesis." error message here:

if (!stack.isempty() && stack.peek().equals('(')) {    stack.pop(); // pop/remove left parenthesis } else {    postfix = "there no matching left parenthesis.";    return postfix; } 

Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -