java - Trouble verifying incorrect input in program -


i've written infix postfix calculator. works fine long infix equations valid, can't figure out how handle invalid equations.

my program reads infix equations text file separated line breaks. prints results of converted postfix problems new text file. trying have if 1 equation invalid, instead of breaking program prints "invalid" on output page. i'm not sure do!

my idea create case in list of switches mentioned "all other chars" somehow, since char isn't operator or operand, can't work @ all, , suspicious it's impossible.

public class expressiontools extends consolecalculator { //for infix postfix static stack s; static string in; private string out = "";  /**  * expression tools  * @param input  */ public expressiontools(string input) {     in = input; // input = inputed string     s = new stack<character>(); // stack = stack of chars }  /**  * determines string precedence  * @throws postfixexception  * @throws ioexception  */ public string precedence() throws postfixexception, ioexception {     // check string precedence     // each character in given string     (int = 0; < in.length(); i++) {         char cha = in.charat(i);         switch (cha) {         // if + or -         case '+':         case '-':             operator(cha, 1);             break;         // first level of precedence         // if * or /         case '*':         case '/':             operator(cha, 2); // pop             break;         // second level of precedence         case '(':             s.push(cha); // if open paren, add stack             break;         case ')':             parentheses(cha); // pop operators             break;         default:              out = out + cha;             break;// write output string         }     }      // while stack not empty, add character output     while (!s.isempty()) {         out = out + s.pop();     }     postfix.add(out);     return out; // returns postfix expression }     /**  * sets precedence of operators  *   * @param oper  * @param precedence1  * @throws postfixexception  * @throws ioexception   */ public void operator(char oper, int precedence1) throws postfixexception, ioexception {     // while stack not empty     // check precedence of operators     while (!s.isempty()) {         char head = (char) s.pop();         if (head == '(') { // if bracket, push character             s.push(head);             break;         } else {             int precedence2;             if (head == '+' || head == '-')                 precedence2 = 1;             else                 precedence2 = 2;             // if precedence of new operator             // less precedence of old operator             if (precedence2 < precedence1) {                 s.push(head); // push operator stack                 break;             }             // if precedence not less, print output             else                 out = out + head;         }     }     s.push(oper); }  /**  * if parentheses open paren, ignore else print output  *   * @param c  * @throws postfixexception  * @throws ioexception   */ public void parentheses(char c) throws postfixexception, ioexception {           if(s.isempty()) out = "invalid";      while (!s.isempty()) {         char = (char) s.pop(); //      if(((stack) s.peek()).isempty()){}         if (a == '(')             break;         else             out = out + a;     } } 

i'm pretty lost this!

edit i've tried running input through algorithm put proper format (which number space operator space number etc etc) solves lot of issue, think major part of issue easier discover using stacks.

for example, in last method, parentheses, tried adding:

    if(s.isempty()) out = "invalid"; 

however reason doesn't work, output never prints if if statement occurs , instead error , program stops running, i'm not sure why.


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -