My assignment is to evaluate expressions read in from a file, such as (4-7)/2+6<3*(1-9).
< and > mean minimum and maximum, respectively, and have a higher precedence than / and *.
However, I am getting a NoSuchElementFound exception when attempting to evaluate an expression, pointing towards the stack I return at the end of the evaluate method. I'm assuming this means the stack is empty. I am having trouble seeing where the issue is. Here is my evaluate code. For what it matters, I have implemented my own stack classes (both strings) and am not using generics.
/**
* Evaluates the expression and updates textArea appropriately
*/
private String evaluateLine()
{
char token;
String[] tokens = new String[expression[0].length()];
for(int i = 0; i < expression[0].length(); i++)
{
token = expression[0].charAt(i);
tokens[i] = String.valueOf(token);
}
OperatorStack operators = new OperatorStack();
IntegerStack integers = new IntegerStack();
for(int i = 0; i < tokens.length; i++)
{
if(isInteger(tokens[i]))
{
integers.push(tokens[i]);
}
else if(tokens[i] == "(")
{
operators.push(tokens[i]);
}
else if(tokens[i] == ")")
{
while(operators.top() != "(")
{
integers.push(applyOp(operators.pop(), integers.pop(), integers.pop()));
operators.pop();
}
}
else if(tokens[i] == "+" || tokens[i] == "-" || tokens[i] == "*" || tokens[i] == "/"
|| tokens[i] == "<" || tokens[i] == ">")
{
while(!operators.isEmpty() && hasPrecedence(tokens[i], operators.top(), operators.top()))
{
integers.push(applyOp(operators.pop(), integers.pop(), integers.pop()));
}
operators.push(tokens[i]);
}
}
while(!operators.isEmpty())
{
integers.push(applyOp(operators.pop(), integers.pop(), integers.pop()));
}
return integers.pop();
}
public static boolean hasPrecedence(String op1, String op2, String op3)
{
if(op2 == "(" || op2 == ")")
{
return false;
}
if((op1 == "<" || op1 == ">") && (op2 == "*" || op2 == "/") && (op3 == "+" || op3 == "-"))
{
return false;
}
else
{
return true;
}
}
public static String applyOp(String op, String b, String a)
{
switch (op)
{
case "+":
return Integer.toString(Integer.parseInt(a) + Integer.parseInt(b));
case "-":
return Integer.toString(Integer.parseInt(a) - Integer.parseInt(b));
case "*":
return Integer.toString(Integer.parseInt(a) * Integer.parseInt(b));
case "/":
if(Integer.parseInt(b) == 0)
{
throw new
UnsupportedOperationException("Cannot divide by 0");
}
return Integer.toString(Integer.parseInt(a) / Integer.parseInt(b));
case "<":
if(Integer.parseInt(a) < Integer.parseInt(b))
{
return b;
}
else if(Integer.parseInt(b) < Integer.parseInt(a))
{
return a;
}
case ">":
if(Integer.parseInt(a) > Integer.parseInt(b))
{
return a;
}
else if(Integer.parseInt(b) > Integer.parseInt(a))
{
return b;
}
}
return Integer.toString(0);
}
public boolean isInteger(String str)
{
return str.matches("0123456789");
}
Hard to find the exact problem because you do not include enough code to get your example to compile but one thing I notice is that your isInteger
method does not work. When I ran the input into your method it produced an empty stack for the integers. Use this instead:
public boolean isInteger(String str) {
try {
Integer.parseInt(str);
return true;
} catch (NumberFormatException ex) {
return false;
}
}
Alternatively I think you were attempting to use a Regex for determining if the string contains an integer. For that you would need to do this:
public boolean isInteger(String str) {
return str.matches("[0-9]+");
}
With either of these implementations of isInteger
I was able to get your code to run when I replace your stack with Stack<String>
collections. Hope that helps.