Enter an Expression:
A[F + X {Y 2}]
The expression is Legal
Enter an Expression:
B+[3 {X/2})*19 + 2/(X 7)
ERROR] expected
Enter an Expression:
()) (
ERROR--) without (
$
I created a class called BalancedExpression and a driver called ExpressionChecker. I completed my BalancedExpression class. But, I'm having trouble setting up my driver to print out an expression like the sample above. The only thing I was able to figure out was how to terminate my program by letting the user enter a $. As for letting the user enter an expression, my output repeats "Enter an Expression:" instead of "legal" or "ERROR".
Here is my code so far:
public class BalancedExpression
{
public BalancedExpression() // Default Constructor
{
sp = 0; // the stack pointer
theStack = new int[MAX_STACK_SIZE];
}
public void push(int value) // Method to push an expression into the stack
{
if (!full())
theStack[sp++] = value;
}
public int pop() // Method to pop an expression out of the stack
{
if (!empty())
return theStack[--sp];
else
return -1;
}
public boolean full() // Method to determine if the stack is full
{
if (sp == MAX_STACK_SIZE)
return true;
else
return false;
}
public boolean empty() // Method to determine if the stack is empty
{
if (sp == 0)
return true;
else
return false;
}
public static boolean checkExpression(String ex) // Method to check Expression in stack
{
BalancedExpression stExpression = new BalancedExpression();
for(int i = 0; i < MAX_STACK_SIZE; i++)
{
if (ex.charAt(i) == LEFT_PAREN)
stExpression.push(LEFT_PAREN);
if (ex.charAt(i) == LEFT_BRACE)
stExpression.push(LEFT_BRACE);
if (ex.charAt(i) == LEFT_BRACKET)
stExpression.push(LEFT_BRACKET);
if (ex.charAt(i) == RIGHT_PAREN) {
if (stExpression.empty())
return false;
if (stExpression.pop() != LEFT_PAREN)
System.out.print("ERROR( expected ");
return false;
}
else if (ex.charAt(i) == RIGHT_BRACE) {
if (stExpression.empty())
return false;
if (stExpression.pop() != LEFT_BRACE)
System.out.print("ERROR{ expected ");
return false;
}
else if (ex.charAt(i) == RIGHT_BRACKET) {
if (stExpression.empty())
return false;
if (stExpression.pop() != LEFT_BRACKET)
System.out.print("ERROR[ expected ");
return false;
}
}
return stExpression.empty();
}
private int sp;
private int[] theStack;
private static final int MAX_STACK_SIZE = 6;
private static final char LEFT_PAREN = '(';
private static final char RIGHT_PAREN = ')';
private static final char LEFT_BRACE = '{';
private static final char RIGHT_BRACE = '}';
private static final char LEFT_BRACKET = '[';
private static final char RIGHT_BRACKET = ']';
}// End of class BalancedExpression
// My Driver
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class ExpressionChecker
{
public static void main(String[] args)
{
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader console = new BufferedReader(reader);
BalancedExpression exp = new BalancedExpression();
String expression = "";
do
{
try{
System.out.print("Enter an Expression: ");
expression = console.readLine();
if("$".equals(expression))
break;
else
exp.checkExpression(expression);
}catch(Exception e){
System.out.println("IO error:" + e);
}
}while(!expression.equals(""));// End of while loop
}
}// End of class ExpressionChecker
Can anyone please help me develop my driver program to print out an output similar to the sample example? Any help is appreciated. Thanks!

New Topic/Question
Reply



MultiQuote



|