Note that there are some explanatory texts on larger screens.

plurals
  1. POmatching parentheses code using stack
    primarykey
    data
    text
    <p>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:</p> <pre><code> public interface Stack&lt;E&gt;{ public int size(); public boolean isEmpty(); public E top(); public void push(E element); public E pop()throws EmptyStackException; } </code></pre> <p>And this is the class named MyStack which emplements stack:</p> <pre><code> public class myStack&lt;E&gt; implements Stack&lt;E&gt;{ 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; } } </code></pre> <p>and this is the main:</p> <pre><code> public static void main(String[] args) { Stack&lt;String&gt; st=new myStack&lt;&gt;(); Scanner s = new Scanner(System.in); String str; str = s.nextLine(); for(int i=0;i&lt;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"); } </code></pre> <p>But the main code has error in these line: <code>st.push(str.charAt(i));</code> And <code>if((st.top()==str.charAt(i)))</code>. the Error is about converting char to String.</p> <p>can any one please help me to solve these problems??</p> <p>sorry I know this long code is boring But I really need to solve this problem</p> <p>Thanks for your attention in advance</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload