Using switch-case for better readability and handling of other scenarios:
import java.util.Scanner;
import java.util.Stack;
public class JavaStack
{
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String input = sc.next();
System.out.println(isStringBalanced(input));
}
scanner.close();
}
private static boolean isStringBalanced(String testString)
{
Stack<Character> stack = new Stack<Character>();
for (char c : testString.toCharArray()) {
switch (c) {
case '[':
case '(':
case '{':
stack.push(c);
break;
case ']':
if (stack.isEmpty() || stack.pop() != '[') {
return false;
}
break;
case ')':
if (stack.isEmpty() || stack.pop() != '(') {
return false;
}
break;
case '}':
if (stack.isEmpty() || stack.pop() != '{') {
return false;
}
break;
default:
break;
}
}
// stack has to be empty, if not, the balance was wrong
return stack.empty();
}
}