Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Stack Practice: Evaluating Expressions Syntax

  1. #1
    Member Staticity's Avatar
    Join Date
    Jul 2011
    Location
    Texas
    Posts
    105
    My Mood
    Inspired
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default Stack Practice: Evaluating Expressions Syntax

    Alright, so I'm a CS1 student.. who self taught himself all of the CS1 curriculum... and is now working on CS2 work. So I'm kind of out of my league on this one, without much guidance.. (That's why I love this site!) So... CS2 is currently practicing basic uses of Stacks with Expressions... Here is an example of Input/Output:

    Input:
    (hey)
    Output:
    true

    Input:
    ([a+1])
    Output:
    false

    Input:
    ( { ] )
    Output:
    false

    So, it's basically just checking if the correct "close" symbol matches up with the previous "open" symbol.. Here is my Code:

    	public boolean checkSyntax2(String s){
     
    		Stack<Character> stack = new Stack<Character>();
     
    		char[] open = {'(', '[', '{', '<'};
    		char[] close = {')', ']', '}', '>'};
    		char c = '-';
     
    		for(int a = 0; a < s.length(); a++){
    			for(int i = 0; i < open.length;i++){
    				if(s.charAt(a) == open[i]){
    					c = open[i];
    					stack.push(s.charAt(a));
    				}
    				else if(s.charAt(a) == close[i]){
    					if(!stack.isEmpty() && c == stack.peek())
    						stack.pop();
    				}
    			}
    		}
    			if(stack.isEmpty())
    		return true;
    			return false;
    	}

    My Code works in all cases except for one like so: "([])"... As you can see within the first IF statement within the Nested For Loop, I have a line that says to set the char c equal to open[i]... c = open[i].. Well, this doesn't retain c, so if I have "([])", it pushes the "(" on the stack, makes c="(", then pushes "[" on to the stack and makes c="["... So the char c was overwritten. How can I retain each open character I have? I know I could use an ArrayList, but I don't exactly see how it can work that way.. Also I think a second Stack could be helpful, but I believe my CS teacher told me you would only need one Stack per method.. Sorry, I know it's a lot of information, but any help would be Greatly Appreciated!
    Simplicity calls for Complexity. Think about it.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Stack Practice: Evaluating Expressions Syntax

    c isn't necessary at all, or another way to think about it is c is always whatever is on top of the stack.

    Algorithm:

    1. For each character in the string
    a. If it's an open character, push it onto the stack
    b. If it's a close character, try to pop the top item off the stack. If you can't or it doesn't match the close character, there's a problem.
    2. If after iterating through every character there are still items left on the stack, there's a problem.

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    Staticity (October 4th, 2011)

  4. #3
    Member Staticity's Avatar
    Join Date
    Jul 2011
    Location
    Texas
    Posts
    105
    My Mood
    Inspired
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default Re: Stack Practice: Evaluating Expressions Syntax

    Quote Originally Posted by helloworld922 View Post
    c isn't necessary at all, or another way to think about it is c is always whatever is on top of the stack.
    Oh haha! take out C, and in the else if statement, just check if open[i] = stack.peek() !

    Thanks a lot HelloWorld! I have no idea why my teacher suggested a character to be placed in there :/
    Simplicity calls for Complexity. Think about it.

Similar Threads

  1. [SOLVED] evaluating postfix expressions using stack
    By lieles in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 16th, 2011, 11:48 AM
  2. Evaluating Expressions
    By javapenguin in forum What's Wrong With My Code?
    Replies: 19
    Last Post: November 16th, 2010, 08:30 PM
  3. VM re-evaluating a string
    By Johannes in forum Java Theory & Questions
    Replies: 6
    Last Post: September 20th, 2010, 04:44 PM
  4. why is this statement evaluating false??
    By humdinger in forum Object Oriented Programming
    Replies: 2
    Last Post: November 3rd, 2009, 04:28 PM
  5. Evaluating to negative zero
    By helloworld922 in forum Java Theory & Questions
    Replies: 6
    Last Post: June 25th, 2009, 02:34 PM