1 Replies - 183 Views - Last Post: 09 October 2012 - 02:57 PM Rate Topic: -----

#1 j0701  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 09-October 12

Program that reads expressions that contain [ ], { } ,and ()

Posted 09 October 2012 - 02:12 PM

I'm supposed to create a Java program which reads expressions that contain, among other items, braces { }, brackets [ ], and parentheses ( ). My program should be properly nested and that ‘(‘ matches ‘)’, ‘[‘ matches ‘]’, and ‘{’ matches ‘}‘. The program should be terminated by a ‘$’ at the beginning of an input line. These are supposed to be sample runs of my program:

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!

Is This A Good Question/Topic? 0
  • +

Replies To: Program that reads expressions that contain [ ], { } ,and ()

#2 rfs02  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 26
  • View blog
  • Posts: 70
  • Joined: 30-September 12

Re: Program that reads expressions that contain [ ], { } ,and ()

Posted 09 October 2012 - 02:57 PM

The only time you print anything to the console is when you find unmatched opening and closing "identifiers".

Otherwise, you just return true or false - which doesn't print anything.

If you enter something like "{)", it will print an error.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1