Results 1 to 10 of 10
  1. #1
    msa0127a is offline Member
    Join Date
    Oct 2010
    Location
    Baltimore, Maryland
    Posts
    5
    Rep Power
    0

    Default matching parentheses stack

    Hello,
    I'm new to Java and I have to write a program that reads in a string and matches the grouping symbols. If they don't match it has to return false and the position where the mismatch occurred. I got everything except the positioning part. That's where I'm stuck.
    Any help would be appreciated.

    here is my code:

    import java.util.*;
    import java.util.Scanner;
    import java.util.Stack;

    public class Assignment3a
    {
    private static final char LEFTPARENT = '(';
    private static final char RIGHTPARENT = ')';
    private static final char LEFTBRACE = '{';
    private static final char RIGHTBRACE = '}';
    private static final char LEFTBRACKET = '[';
    private static final char RIGHTBRACKET = ']';
    //declaring Parenthesis, Curly Braces and Brackets (left and right)
    public static boolean isBalanced(String s) //if the input string is balanced (true/false)
    {
    Stack<Character> stack = new Stack<Character>();//
    int c=0;
    for (int i = 0; i < s.length(); i++) //reading the string into the loop

    {

    if (s.charAt(i) == LEFTPARENT) stack.push(LEFTPARENT); //push the first left parenthesis onto the stack

    else if (s.charAt(i) == LEFTBRACE) stack.push(LEFTBRACE); //push the first left Curly Brace onto the stack

    else if (s.charAt(i) == LEFTBRACKET) stack.push(LEFTBRACKET); //push the first left Bracket onto the stack

    else if (s.charAt(i) == RIGHTPARENT)
    {
    if (stack.isEmpty())
    return false;
    if (stack.pop() != LEFTPARENT)
    return false;
    }

    else if (s.charAt(i) == RIGHTBRACE)
    {
    if (stack.isEmpty())
    return false;
    if (stack.pop() != LEFTBRACE)
    return false;
    }

    else if (s.charAt(i) == RIGHTBRACKET)
    {
    if (stack.isEmpty())
    return false;
    if (stack.pop() != LEFTBRACKET)
    return false;

    }

    // all other characters will be ignored
    }

    return stack.isEmpty();


    }
    public static void main(String args[])
    {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter a string with Parentheses, Brackets and curly braces: ");
    String s = input.next();

    {
    if (isBalanced(s)== true)
    System.out.println("The Parenthesis in"+" \""+ s +"\" "+"match");
    if (isBalanced(s)== false)
    System.out.println("The Parenthesis in"+" \""+ s +"\" "+ "do not match");
    System.out.println();
    }
    }
    }

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    First tip would be to post your code in code tags, it makes it more readable. Next, to report the location where the input is erroneous, I'd change your method signature to int instead of boolean, and instead of returning false, you return the index, if the input is valid, return -1.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  3. #3
    msa0127a is offline Member
    Join Date
    Oct 2010
    Location
    Baltimore, Maryland
    Posts
    5
    Rep Power
    0

    Default

    Thanks for the quick reply m00nchile. Sorry, that was my first post. My next one I will post in code tags. I forgot to mention that I have to use boolean. That's what makes it so confusing for me.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by msa0127a View Post
    Thanks for the quick reply m00nchile. Sorry, that was my first post. My next one I will post in code tags. I forgot to mention that I have to use boolean. That's what makes it so confusing for me.
    Silly requirement ... but make you loop counter 'i' a member variable for now so that your other method can retrieve it in case of a parse error; b.t.w. you're always parsing your String twice in your main method, there's no need to do so.


    kind regards,

    Jos

  5. #5
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Another way to circumvent the boolean requirement and a member variable would be to just print out the index where parsing wasn't succesfull:
    Java Code:
    if(conditionNotMet) {
      System.out.println("Parse error at "+i);
      return false;
    }
    Of course, this is assuming you don't need the parse error location anywhere else. And I agree with JosAH, programming classes can have very silly requirements for their assignments. And also, JosAHs observation on double parsing:
    Java Code:
    if (isBalanced(s)) //already a boolean, don't need the == true
    System.out.println("The Parenthesis in"+" \""+ s +"\" "+"match");
    else //don't need to check again
    System.out.println("The Parenthesis in"+" \""+ s +"\" "+ "do not match");
    System.out.println();
    Ever seen a dog chase its tail? Now that's an infinite loop.

  6. #6
    msa0127a is offline Member
    Join Date
    Oct 2010
    Location
    Baltimore, Maryland
    Posts
    5
    Rep Power
    0

    Default

    Thanks JosAH and m00nchile. I know this sounds a bit stupid, but I'm really new to Java. How do I make "i" a member and where do I place that if-statement then? I fixed the double parsing and learned something again.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by msa0127a View Post
    Thanks JosAH and m00nchile. I know this sounds a bit stupid, but I'm really new to Java. How do I make "i" a member and where do I place that if-statement then? I fixed the double parsing and learned something again.
    Move the declaration of 'i' outside of your method; like this:

    Java Code:
    public class Assignment3a 
    {
    private int i; // position in the String to parse
    ...
    }
    kind regards,

    Jos

  8. #8
    msa0127a is offline Member
    Join Date
    Oct 2010
    Location
    Baltimore, Maryland
    Posts
    5
    Rep Power
    0

    Default

    That was a great hint from you guys. Thank You m00nchile and Dank U wel JosAH. I made "i" private, but how do I retrieve the position in main?

  9. #9
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Make a getter method to fetch the i variable:
    Java Code:
    public class Example {
      private int i;
      //constructors... methods...
      public int getI() {
        return i;
      }
    }
    
    public class ExampleTest {
      public static void main(String[] args) {
        Example e = new Example();
        //do the parsing
        //if input is erroneous
        System.out.println("Mistake at " + e.getI());
      }
    }
    I usually don't post solutions, but you obviously know a bit of programming, and things like these are just semantic issues.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  10. #10
    msa0127a is offline Member
    Join Date
    Oct 2010
    Location
    Baltimore, Maryland
    Posts
    5
    Rep Power
    0

    Default

    Thanks for your help m00nchile. I'm coming from C. I didn't know that Java is so much different and more advanced than C. However, I appreciate your efford to help me out on this one.It seems everything I try, I get a compiler error. I just turn it in the way it is. At least the boolean is working.
    Thank you for your time and patience.

Similar Threads

  1. Using regex to retrieve all text inside parentheses
    By adhoc334 in forum Advanced Java
    Replies: 5
    Last Post: 08-18-2010, 08:05 PM
  2. Paranthesis Matching???
    By MuslimCoder in forum New To Java
    Replies: 1
    Last Post: 10-27-2009, 06:02 AM
  3. matching and getting xml data
    By Juuno in forum Advanced Java
    Replies: 6
    Last Post: 04-26-2009, 06:25 PM
  4. Help with signature matching
    By cachi in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 08:21 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •