heyy..
i am getting error on line 51 "Stack<Character> stack = new Stack<Character>();"..how can i fix it? please help.
import java.io.IOException;
import java.util.Scanner;
public class SymbolChecker {
private String enter;
public SymbolChecker(String in) {
enter = in;
}
Scanner input = new Scanner(System.in);
public void check() {
int stackSize = enter.length();
Stack theStack = new Stack(stackSize);
for (int j = 0; j < enter.length(); j++)
{
char ch = enter.charAt(j);
switch (ch) {
case '{': // opening symbols
case '[':
case '(':
theStack.push(ch); // push them
break;
case '}': // closing symbols
case ']':
case ')':
if (!theStack.isEmpty()) // if stack not empty,
{
char chx = theStack.pop(); // pop and check
if ((ch == '}' && chx != '{') || (ch == ']' && chx != '[')
|| (ch == ')' && chx != '('))
System.out.println("Error: " + ch + " at " + j);
} else
// prematurely empty
System.out.println("Error: " + ch + " at " + j);
break;
default: // no action on other characters
break;
}
}
if (!theStack.isEmpty())
System.out.println("Error: missing right delimiter");
}
public static void main(String[] args) throws IOException {
String input = "{Java [Source] (and) {[(Support)]}}";
SymbolChecker theChecker = new SymbolChecker(input);
theChecker.check();
Stack<Character> stack = new Stack<Character>();
String s = "{[]}{{}]";
//s = "{}{}{[]}";
int index = 0;
while(index < s.length()) {
char ch = s.charAt(index);
if(ch == '{' || ch == '[' || ch == '(') {
stack.push(ch);
} else {
if(stack.empty()) {
System.out.println("error");
continue;
}
// pop an item from stack
char ch1 = stack.pop();
// check if it's a matching pair
if(ch1 == '{' && ch != '}' ||
ch1 == '[' && ch != ']' || ch1 == '(' && ch != ')') {
System.out.println("error unmatched " + ch);
}
}
index++;
}
if(!stack.empty()) {
System.out.println("unbalanced symbols");
}
}
}
class Stack {
private int maxSize;
private char[] stackArray;
private int top;
public Stack(int max) {
maxSize = max;
stackArray = new char[maxSize];
top = -1;
}
public void push(char j) {
stackArray[++top] = j;
}
public char pop() {
return stackArray[top--];
}
public char peek() {
return stackArray[top];
}
public boolean isEmpty() {
return (top == -1);
}
}