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 1 of 1

Thread: help for stacks

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default help for stacks

    Hello,

    I'm new to Java. I've written a simple program to match parentheses, curly braces and brackets. It works fine, but I can't figure out how to print out the position when a mismatch occurs.
    For example: "(ab[c)" is should return that the mismatch is at position 3.
    Any help would be appriciated.

    Here is my code so far:

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

    public class Assignment3

    {
    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>(); //
    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"+" ");
    }
    }
    }
    Last edited by msa0127a; October 3rd, 2010 at 12:16 AM.


Similar Threads

  1. Palindrome Stacks
    By mgutierrez19 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 7th, 2010, 03:05 AM

Tags for this Thread