-
Notifications
You must be signed in to change notification settings - Fork 171
/
Copy pathBracketsApp.java
executable file
·112 lines (93 loc) · 2.38 KB
/
BracketsApp.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import java.util.Scanner;
class Stack {
private Character[] a;
private int ele;
private int size;
public Stack(int size) {
a = new Character[size];
this.size = size;
ele = -1;
}
public void push(Character ch) {
if (isFull()) {
System.out.println("Stack is Full");
} else {
ele++;
a[ele] = ch;
}
}
public Character pop() {
if (isEmpty()) {
System.out.println("Stack is Empty");
return '\0';
} else {
Character ch = a[ele];
ele--;
return ch;
}
}
public boolean isEmpty() {
if(ele == -1) return true;
else return false;
}
public boolean isFull() {
if(ele == size - 1) return true;
return false;
}
}
class BracketChecker {
private String input;
public BracketChecker(String in) {
input = in;
}
public void check() {
int stackSize = input.length();
Stack theStack = new Stack(stackSize);
for(int j=0; j<input.length(); j++) {
char ch = input.charAt(j);
switch(ch) {
case '{':
case '[':
case '(':
theStack.push(ch);
break;
case '}':
case ']':
case ')':
if( !theStack.isEmpty() ) {
char chx = theStack.pop();
if( (ch=='}' && chx!='{') ||
(ch==']' && chx!='[') ||
(ch==')' && chx!='(') )
System.out.println("Error: "+ch+" at "+j);
}
else {
System.out.println("Error: "+ch+" at "+j);
}
break;
default:
break;
}
}
if( !theStack.isEmpty() ) {
System.out.println("Error: missing right delimiter");
}
else {
System.out.println("No error");
}
}
}
class BracketsApp {
public static void main(String[] args) {
String input;
Scanner sc = new Scanner(System.in);
System.out.print("Enter string containing delimiters: ");
input = sc.nextLine();
if( input == "" ) {
System.out.println("");
} else {
BracketChecker theChecker = new BracketChecker(input);
theChecker.check();
}
}
}