this is my code what is problem in this code
public static boolean isBalanced(String s, StacksUsingArrays p) throws Exception {
for(int i=0;i<s.length();i++) {
if(s.charAt(i)==β(β||s.charAt(i)==β{β||s.charAt(i)==β[β){
p.push(s.charAt(i));
}else{
if(p.isEmpty())
return false;
if(s.charAt(i)==β)β && p.top()!=β(β ||
s.charAt(i)==β}β&&p.top()!=β{β||
s.charAt(i)==β]β&& p.top()!=β[β) {
return false;
}
p.pop();
}
}
return p.isEmpty();
}
Some test case are give run time error in balanced paranthesis
import java.util.*; public class Main { public static void main(String[] args) throws Exception { // TODO Auto-generated method stub Scanner s = new Scanner(System.in); String str = s.next(); Main mainobj = new Main(); StacksUsingArrays stack = mainobj.new StacksUsingArrays(1000); if (isBalanced(str, stack)) { System.out.println(βYesβ); } else { System.out.println(βNoβ); } } public static boolean isBalanced(String s, StacksUsingArrays p) throws Exception { for(int i=0;i<s.length();i++) { if(s.charAt(i)==β(β||s.charAt(i)==β{β||s.charAt(i)==β[β){ p.push(s.charAt(i)); }else{ if(p.isEmpty()) return false; if(s.charAt(i)==β)β && p.top()!=β(β || s.charAt(i)==β}β&&p.top()!=β{β|| s.charAt(i)==β]β&& p.top()!=β[β) { return false; } p.pop(); } } return p.isEmpty(); } private class StacksUsingArrays { private int[] data; private int tos; public static final int DEFAULT_CAPACITY = 10; public StacksUsingArrays() throws Exception { // TODO Auto-generated constructor stub this(DEFAULT_CAPACITY); } public StacksUsingArrays(int capacity) throws Exception { if (capacity <= 0) { System.out.println(βInvalid Capacityβ); } this.data = new int[capacity]; this.tos = -1; } public int size() { return this.tos + 1; } public boolean isEmpty() { if (this.size() == 0) { return true; } else { return false; } } public void push(int item) throws Exception { if (this.size() == this.data.length) { throw new Exception(βStack is Fullβ); } this.tos++; this.data[this.tos] = item; } public int pop() throws Exception { if (this.size() == 0) { throw new Exception(βStack is Emptyβ); } int retVal = this.data[this.tos]; this.data[this.tos] = 0; this.tosβ; return retVal; } public int top() throws Exception { if (this.size() == 0) { throw new Exception(βStack is Emptyβ); } int retVal = this.data[this.tos]; return retVal; } public void display() throws Exception { if (this.size() == 0) { throw new Exception(βStack is Emptyβ); } for (int i = this.tos; i >= 0; iβ) { System.out.println(this.data[i]); } } } }
sir question ho gaya