Linda
Linda

Reputation: 251

matching parentheses code using stack

I want to write a program to be able to receive a String from the Standard input and Check for matching parentheses. Here is my stack code:

    public interface Stack<E>{
    public int size();
    public boolean isEmpty();
    public E top();
    public void push(E element);
    public E pop()throws EmptyStackException;
    }

And this is the class named MyStack which emplements stack:

    public  class myStack<E> implements Stack<E>{
    private final E s[];
    int t=0;
    public myStack() {
    this.s = (E[]) new Object[100];
    }
    public int size(){
    return t;
    }

    public boolean isEmpty(){
    switch(size()){
        case 0:
            return true;
    }
    return false;
    }

    public E top() {
    if(isEmpty())
        throw new EmptyStackException();
    return s[t-1];
    }


    public void push(E element) {
     if(isEmpty())
        s[0]= element;
     else
        s[t]= element;
     t++;
    }
    public E pop() {
    E x;
    if(isEmpty())
        throw new EmptyStackException();
    else{
        x = s[t-1];
        s[t-1] = null;
        t--;
    }
    return x;
    }

    }

and this is the main:

      public static void main(String[] args) {

      Stack<String> st=new myStack<>(); 
      Scanner s = new Scanner(System.in);
      String str;
      str = s.nextLine();
      for(int i=0;i<str.length();i++)
      {
        if((str.charAt(i)=='{')||(str.charAt(i)=='(')||(str.charAt(i)=='['))
        {
            st.push(str.charAt(i));
        }
        else if((str.charAt(i)=='}')||(str.charAt(i)==')')||(str.charAt(i)==']'))
            if((st.top()==str.charAt(i)))
                    st.pop();
            else 
            {
                System.out.println("Error");
                System.exit(0);
            }
     }


     if(st.isEmpty())
        System.out.println("True");
     else
        System.out.println("True");
     }

But the main code has error in these line: st.push(str.charAt(i)); And if((st.top()==str.charAt(i))). the Error is about converting char to String.

can any one please help me to solve these problems??

sorry I know this long code is boring But I really need to solve this problem

Thanks for your attention in advance

Upvotes: 1

Views: 317

Answers (2)

JustWannaFly
JustWannaFly

Reputation: 301

I see two options here. The first was stated and is using a stack of characters. The other is to cast the char to Character and then call the .toString() method before you push to the stack.

Your push would look something like:

st.push(((Character)str.charAt(i)).toString());

and your pop:

if((st.top().equals(((Character)str.charAt(i)).toString()))
    st.pop();

You might have to use the .equals(Object) method to properly determine if the strings/chars are equal. == will only look at the references in some cases which will return false when the references are to different places in memory even though the contents of the strings is identical.

If you use the stack specifically as strings elsewhere I would keep it a string, otherwise it is cleaner to simply use a stack of characters

Stack<Character> st = new myStack<>();

or

Stack<char> st = new myStack<>();

Upvotes: 1

Tilo
Tilo

Reputation: 3325

You should use a stack of characters in your main method:

Stack<Character> st = new myStack<>();

Once you get it to compile you will also find some mistakes with your logic. For example in the expression (aa) when you read the ) you have ( on the stack, so you cannot just compare, but need to account for that.

Upvotes: 2

Related Questions