Hello all,

I am trying to make a program that essentially takes a string of parenthesis and outputs whether or not its balanced.

Basically, every '(' will push the index of that location of the string on to the stack, while a ')' will pop it off if it's not empty. At the end, it will peek at the stack giving me the index of the first offending parenthesis. My problem now is the code below isn't working correctly... can anyone assist me in helping me figure out what i am doing wrong?? The negative one output means it is balanced correctly.
Thank you

-prince

public static int parenError( String theInput ) {
String input = theInput;
    	
    	int output = 0;
    	Deque<Integer> stack = new ArrayDeque<Integer>();
    	for (int i = 0; i < input.length(); i++)
    	{
    		char c = input.charAt(i);
    		if (c == '(')
    		{
    			stack.push(input.indexOf(i));
    		}
    		else if (c == ')')
    		{
    			if (!stack.isEmpty())
    			{
        			stack.pop();
    			}
    		}
    		else
    		{
    			System.out.println("nothing");
    		}
    	}
    	
    	if (!stack.isEmpty())
    	{
    		output = stack.peek();
    	}
    	else if (stack.isEmpty())
    	{
    		output = -1;
    	}
    	
    	return output;
	 }
}

Edited 4 Years Ago by skylinepkr: n/a

sorry i forgot, this is another class:

public class TestWellFormed {
    public static void main( String[] args ) {
        TestData[] someParens = { new TestData( "((())())()", -1 ),
                                  new TestData( ")()(", 0),
                                  new TestData( "())", 2),
                                  new TestData( "(()()(()", 0),
                                  new TestData( "(()())(()", 6),
                                  new TestData( "(()()())(", 8),
                                  new TestData( "(()()()", 0)
                                };
        
        for( int i = 0; i < someParens.length; i++ ) {
            System.out.print( someParens[i].testInput + " gives " );        
            System.out.print( WellFormedStub.parenError( someParens[i].testInput ) );
            System.out.println( ", should give " + someParens[i].expectedErrorValue );

        }
    }    
}
class TestData {
    public String testInput;
    public int expectedErrorValue;
    TestData( String str, int err ) {
          testInput = str;
        expectedErrorValue = err;
    }
}

Ok 1 suggestion before I try to look into the problem with your code. See to it that members of your class TestData are private and not public. Write getter() and setter() method to retrieve and set them. Instead of directly using them as someParens.testInput , using the getter method to retrieve them is a much better idea. What you have done is not an error, but it is usually avoided.

well its the instructors code and i dont have to worry about that. I just need to make my code work correctly which is the first class i posted. It keeps returning -1. I can't figure out how to return the index of the string being looked at when its being pushed. if that makes any sense

stack.push(input.indexOf(i));

This pushes -1 always. indexOf() return first occurrence of character within the string. I think you should be doing stack.push(i);

When indexOf() does not find the character in the string it returns -1. When you say input.indexOf(i) it means you are trying to find first occurrence of i in the string input. Since this is always false in your case, it always returns -1. Hence -1 is pushed always.

This article has been dead for over six months. Start a new discussion instead.