List of usage examples for java.util EmptyStackException printStackTrace
public void printStackTrace()
From source file:Main.java
public static void main(String args[]) { Stack st = new Stack(); System.out.println("stack: " + st); st.push(new Integer(42)); System.out.println("push(" + 42 + ")"); System.out.println("stack: " + st); System.out.print("pop -> "); Integer a = (Integer) st.pop(); System.out.println(a);//from w w w.ja v a2s. c o m System.out.println("stack: " + st); try { st.pop(); } catch (EmptyStackException e) { e.printStackTrace(); System.out.println("empty stack"); } }
From source file:ch.zhaw.icclab.tnova.expressionsolver.OTFlyEval.java
String evaluateExpression(String exp, Stack<Double> param, double threshold) { String[] literals = exp.split("(?!^)"); //array of characters String tempVal = ""; Stack<String> sTable = new Stack<String>(); for (int i = 0; i < literals.length; i++) { if (literals[i].equals("(")) { if (tempVal.trim().length() != 0) { sTable.push("fn " + tempVal.trim()); logger.info("pushed function [" + tempVal.trim() + "] into stack."); } else { //parsing error this stage is not allowed }//from w w w. j a v a 2 s . co m tempVal = ""; } else if (literals[i].equals(",")) { if (tempVal.trim().length() != 0) { sTable.push("pm " + tempVal.trim()); logger.info("pushed parameter [" + tempVal.trim() + "] into stack."); } else { //parsing error this stage is not allowed } tempVal = ""; } else if (literals[i].equals(")")) { if (tempVal.trim().length() != 0) { sTable.push("pm " + tempVal.trim()); logger.info("pushed parameter [" + tempVal.trim() + "] into stack."); } logger.info("Proceeding for partial stack evaluation."); tempVal = ""; //proceed to partial evaluation try { String output = partialEvaluation(sTable, param, threshold); //push the result back into stack as a literal sTable.push("pm " + output); logger.info("pushed parameter [" + output + "] into stack for further processing."); } catch (EmptyStackException emex) { logger.warn("Malformed expression and value set received."); if (App.showExceptions) emex.printStackTrace(); return null; } } else tempVal += literals[i]; } //if stack has more than 1 element error, else the only element is the result String result = ""; if (sTable.size() != 1) { //error result = null; } else { result = sTable.pop().split(" ")[1]; } return result; }