Empty Stack Exception in Java -



Empty Stack Exception in Java -

i'm not sure i'm going wrong code, hoping more experienced eyes review me.

this stack trace:

input look string:(+ 1 2) evaluate look #1 :(+ 1 2) exception in thread "main" java.util.emptystackexception @ java.util.stack.peek(stack.java:102) @ pj2.simplelispexpressionevaluator.evaluatecurrentoperation(simplelispexpressionevaluator.java:126) @ pj2.simplelispexpressionevaluator.evaluate(simplelispexpressionevaluator.java:235) @ pj2_test.main(pj2_test.java:42) java result: 1

this line 126:

while(tokenstack.peek() instanceof double){

this line 235:

evaluatecurrentoperation();

this current code:

import java.util.*; public class stackcalculator{ private string currentexpression; // main look stack, see algorithm in evaluate() private stack<object> tokenstack; public simplelispexpressionevaluator() { tokenstack = new stack(); currentexpression = ""; } // constructor input look public simplelispexpressionevaluator(string inputexpression) { tokenstack = new stack(); tokenstack.push(inputexpression); currentexpression = inputexpression; } public void reset(string inputexpression) { currentexpression = inputexpression; //tokenstack.clear(); //tmpstack.clear(); //not working reason } // function evaluates current operator operands // see finish algorithm in evaluate() // // main steps: // pop operands tokenstack , force them onto // tmpstack until find operator // apply operator operands on tmpstack // force result tokenstack // private void evaluatecurrentoperation() { stack<double> tmpstack=new stack<double>(); double a=0.0; while(!tokenstack.isempty()){ while(!tokenstack.isempty()){ if(tokenstack.peek() instanceof double){ tmpstack.push((double)tokenstack.pop()); }else if(tokenstack.peek() instanceof character){ if(!tokenstack.isempty()){ switch((character)tokenstack.pop()){ case '+': while(!tmpstack.isempty()){ += tmpstack.pop(); } tokenstack.push(a); break; case '-': while(!tmpstack.isempty()){ -= tmpstack.pop(); } tokenstack.push(a); break; case '*': while(!tmpstack.isempty()){ *= tmpstack.pop(); } tokenstack.push(a); break; case '/': while(!tmpstack.isempty()){ /= tmpstack.pop(); } tokenstack.push(a); break; } } } } /** * * algorithm: * * step 1 scan tokens in string. * step 2 if see operand, force operand object onto tokenstack * step 3 if see "(", next token should operator * step 4 if see operator, force operator object onto tokenstack * step 5 if see ")" // steps in evaluatecurrentoperation() : * step 6 pop operands , force them onto tmpstack until find operator * step 7 apply operator operands on tmpstack * step 8 force result tokenstack * step 9 if run out of tokens, value on top of tokenstack result of expression. */ public double evaluate() { scanner currentexpressionscanner = new scanner(currentexpression); double finres = 0.0; currentexpressionscanner = currentexpressionscanner.usedelimiter("\\s*"); // step 1: scan tokens in string. while (currentexpressionscanner.hasnext()) { // step 2: if see operand, force operand object onto tokenstack if (currentexpressionscanner.hasnextint()) { // forcefulness scanner grab of digits // otherwise, 1 char string datastring = currentexpressionscanner.findinline("\\d+"); tokenstack.push(double.parsedouble(datastring)); } else { // next token, 1 char in string token string atoken = currentexpressionscanner.next(); //system.out.println("other: " + atoken); char item = atoken.charat(0); switch (item) { // step 3: if see "(", next token should operator case '(': string nexttoken = currentexpressionscanner.next(); if(!nexttoken.equals("+")&&!nexttoken.equals("- ")&&!nexttoken.equals("*")&&!nexttoken.equals("/")){ throw new lispexpressionexception("after " + item + " should operator"); } break; // step 4: if see operator, force operator object onto tokenstack case '+': tokenstack.push(item); break; case '*': tokenstack.push(item); break; case '-': tokenstack.push(item); break; case '/': tokenstack.push(item); break; // step 5: if see ")" // steps in evaluatecurrentoperation() : case ')': evaluatecurrentoperation(); break; default: // error throw new lispexpressionexception(item + " not legal look operator"); } // end switch } // end else } // end while // step 9: if run out of tokens, value on top of tokenstack // result of expression. // homecoming result while(!tokenstack.isempty()){ if(tokenstack.peek() instanceof character){ throw new lispexpressionexception(tokenstack.pop() + " not number!"); }else{ finres = (double)tokenstack.pop(); } } homecoming finres; // homecoming right answer! }

well, looking @ code, seems quite obvious. if tokenstack contains doubles, empty maintain removing objects it. when it's empty, peek() phone call throw exception.

while(tokenstack.peek() instanceof double){ tmpstack.push((double)tokenstack.pop()); }

java stack lisp calculator

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -