java - Throwing and catching an invalid token -
i'm creating program turns postfix expression infix expression. i've done coming blank on how throw , catch exception of invalid token , in case, invalid token consists of besides number or operator ie: " & ". required throw , catch exception in main class. can catch exception if user enters " & " first input not if isn't entered first. or tips great. thanks.
this actionlistener main class:
public void actionperformed(actionevent arg0) { tree t = new tree(); string postfixexp = inputfield.gettext(); t.buildtree(postfixexp); node root = t.buildtree(postfixexp); answerfield.settext(root.inorderwalk()); } this postfix -> infix method class:
node buildtree(string postfixexp) { stk = new stack<node>(); string[] tokens = postfixexp.split(" "); (string token : tokens) { if (isnumeric(token)) { double d = double.parsedouble(token); node n = new operandnode(d); stk.push(n); } else if (token.equals("+")) { node n1 = stk.pop(); node n2 = stk.pop(); node add = new operatornode(new addoperator(), n2, n1); stk.push(add); } else if (token.equals("-")) { node n1 = stk.pop(); node n2 = stk.pop(); node sub = new operatornode(new suboperator(), n2, n1); stk.push(sub); } else if (token.equals("*")) { node n1 = stk.pop(); node n2 = stk.pop(); node mul = new operatornode(new muloperator(), n2, n1); stk.push(mul); } else if (token.equals("/")) { node n1 = stk.pop(); node n2 = stk.pop(); node div = new operatornode(new divoperator(), n2, n1); stk.push(div); } } node root = stk.pop(); return root; }
Comments
Post a Comment