Balanced Parenthesis

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 str, StacksUsingArrays stack) throws Exception {
for(int i=0;i<str.length();i++){
if(str.charAt(i)=='(' || str.charAt(i)=='{' || str.charAt(i)=='[')
stack.push(i);
else if(str.charAt(i)==')' || str.charAt(i)=='}' || str.charAt(i)==']')	{
if(stack.isEmpty()){
return false;}
else{
int val=stack.pop();
if(val=='['){
if(str.charAt(i)!=']')
return false;	}

if(val=='{'){
if(str.charAt(i)!='}')
return false;	}

if(val=='('){
if(str.charAt(i)!=')')
return false;	}

}
}
}
if(!stack.isEmpty()){
return false;}
return true;}

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]);
		}

	}

}

}

it is showing 2 test case wrong ,what is problem with my code

@Sweta,
You were pushing the index of the string instead of the char. This was the error. https://ide.codingblocks.com/s/197067 Here is corrected code.

Still it is showing two wrong test case
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 str, StacksUsingArrays stack) throws Exception {
for(int i=0;i<str.length();i++){
if(str.charAt(i)=='(' || str.charAt(i)=='{' || str.charAt(i)=='[')
stack.push(str.charAt(i));
else if(str.charAt(i)==')' || str.charAt(i)=='}' || str.charAt(i)==']')	{
if(stack.isEmpty()){
return false;}
else{
int val=stack.pop();
if(val=='['){
if(str.charAt(i)!=']')
return false;	}

if(val=='{'){
if(str.charAt(i)!='}')
return false;	}

if(val=='('){
if(str.charAt(i)!=')')
return false;	}

}
}
}
if(!stack.isEmpty()){
return false;}
return true;}

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]);
		}

	}

}

}

@Sweta,

Please refresh the page and check again. https://ide.codingblocks.com/s/197292 because this code is submitting and giving correct answer.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.