THE ZEPINT NETWORK

programmer assist

Java Java XML Feeds

Java Questions Java Solutions Java Articles

Java is an object-oriented programming language developed initially by James Gosling and colleagues at Sun Microsystems. The language, initially called Oak (named after the oak trees outside Gosling's office), was intended to replace C++, although the feature set better resembles that of Objective C. Java should not be confused with JavaScript, which shares only the name and a similar C-like syntax. Sun Microsystems currently maintains and updates Java regularly.

Java Infix Evaluation Runtime StackUnderflowException Help!

DiggBlinkRedditDeliciousTechnorati

question by ucsb2012 | Easy

import java.util.*;
import java.io.*;

public class ExpressionEvaluation{
public static void main(String[] args){
Scanner fileIn = null;
PrintWriter fileOut = null;
try{
fileOut = new PrintWriter(new FileOutputStream("results.dat"));
}
catch(FileNotFoundException e){
System.out.println("results.dat not found.");
}
String currentElement = null;
//Load File
try{
fileIn = new Scanner(new FileInputStream("expr.dat"));
}
catch(FileNotFoundException e){
System.out.println("Data file expr.dat not found.");
System.exit(0);
}
while(fileIn.hasNextLine()){
String tempLine = fileIn.nextLine();
fileOut.println("Expression: " + tempLine);
double eval = evaluateExpression(tempLine);
fileOut.println("Value: " + eval);
fileOut.println();
}
fileOut.close();
}
// Rerturns an integer representation of an operator's order of precedence
public static int parsePrecedence(String operator){
switch (operator.charAt(0)) {
case '+':
return 2;
case '-':
return 2;
case '*':
return 3;
case '/':
return 3;
case '(':
return 1;
case '[':
return 1;
case '{':
return 1;
default:
return -1;
}
}


// Input: Whole Line from expr.dat
// Output: Double evaluation
public static double evaluateExpression(String expression){
ArrayListStack OperandStack = new ArrayListStack();
ArrayListStack OperatorStack = new ArrayListStack();
String currentElement = null;
StringTokenizer toker = new StringTokenizer(expression);
//Main Loop
int currentOrderOfPrecedence = -1;
while(toker.hasMoreTokens()){
//Read in Next input
currentElement = toker.nextToken();
char ch = currentElement.charAt(0);
// if number
if(Character.isDigit(ch)){
Double.parseDouble(currentElement);
OperandStack.push(currentElement);
}
// else if operator
else if(currentElement.equals("+") || currentElement.equals("-") || currentElement.equals("*") || currentElement.equals("/")){
currentOrderOfPrecedence = parsePrecedence(currentElement);
// pop and evaluate each operator in operator stack above
// last paren & its orderOfPrec>=current
while(parsePrecedence((String)(OperatorStack.top()))>=currentOrderOfPrecedence){
double temp1 = Double.parseDouble((String)(OperandStack.top()));
OperandStack.pop();
double temp2 = Double.parseDouble((String)(OperandStack.top()));
OperandStack.pop();
try{
OperandStack.push(evaluateSimple(temp1,(String)(OperatorStack.top()), temp2));
}
catch(ParenPairException e){
System.out.println("Invalid Parentheses Detected.");
System.exit(0);
}
OperatorStack.pop();
}
OperatorStack.push(currentElement);
}
// else if open paren
else if(currentElement.equals("{") || currentElement.equals("[") || currentElement.equals("<") || currentElement.equals("(")){
OperatorStack.push(currentElement);
}
// else if closed paren
else if(currentElement.equals("}") || currentElement.equals("]") || currentElement.equals(">") || currentElement.equals(")")){
// pop and evaluate all operators until a matching open paren
// is reached, if a non-matching, open paren is reached, throw exception
String matchingParen;
// Determine matching paren
switch(ch){
case '}':
matchingParen = "{";
break;
case ']':
matchingParen = "[";
break;
case '>':
matchingParen = "<";
break;
case ')':
matchingParen = "(";
break;
default:
matchingParen = "";
break;
}
while(!OperatorStack.top().equals(matchingParen)){
double temp1 = Double.parseDouble((String)(OperandStack.top()));
OperandStack.pop();
double temp2 = Double.parseDouble((String)(OperandStack.top()));
OperandStack.pop();
try{
OperandStack.push(evaluateSimple(temp1,(String)(OperatorStack.top()), temp2));
}
catch(ParenPairException e){
System.out.println("Invalid Parentheses Detected.");
System.exit(0);
}
OperatorStack.pop();
}
//Now disregard the matched open paren
OperatorStack.pop();
}
}
return Double.parseDouble((String)(OperatorStack.top()));
}
// Input: operand, operator, operand
// Output: Evaluation of the simple expression
public static double evaluateSimple(double operand1, String operator, double operand2){
switch(operator.charAt(0)){
case '+':
return (operand1 + operand2);
case '-':
return (operand1 - operand2);
case '*':
return (operand1 * operand2);
case '/':
return (operand1 / operand2);
case '(':
throw new ParenPairException("Parentheses do not match up. Error in Data File.");
}
return -1;
}
}

Post reply Subscriptions

Got a Java Question?

Just Sign Up and ask the top Java experts!

Search via Google

User Login

Email Address

Password

Java Experts

Rank Expert Points
#1 Srirangan 100
This a list of the Top Java experts, how many points do you have?

Leading Experts

Rank Expert Points
#1 frankzzsword 4650
#2 Bejaan 2950
#3 csfreak 1100
#4 Anurag 700
#5 keyvez 700
#6 nnarasimha 600
#7 Nakata 600
#8 martinig 600
#9 mastercomputers 550
#10 Abhishek Chatterjee 250
#11 Abels 250
#12 Huntress 150
#13 Adkron 150
#14 Yogesh 100
#15 lexxwern 100
This is a list of overall best performing experts, how many points do you have?