-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParenthesisChecker.java
More file actions
39 lines (35 loc) · 1.27 KB
/
ParenthesisChecker.java
File metadata and controls
39 lines (35 loc) · 1.27 KB
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
/**
Author: Rajin Santos Gajadhar
Student ID: 239479650
Lab 8
Any and all work in this file is my own.
*/
import java.util.Scanner;
public class ParenthesisChecker {
public static void main(String[] args) {
try (Scanner scanner = new Scanner(System.in)) {
System.out.print("Enter a string: ");
String input = scanner.nextLine();
try {
BasedArrayStack stack = new BasedArrayStack(input.length());
for (char ch : input.toCharArray()) {
if (ch == '(') {
stack.push(ch);
} else if (ch == ')') {
if (stack.isEmpty()) {
throw new StackException("Too many closing parentheses");
}
stack.pop();
}
}
if (stack.isEmpty()) {
System.out.println("The string \"" + input + "\" has the correct number of parentheses.");
} else {
System.out.println("The string \"" + input + "\" has too many opening parentheses.");
}
} catch (StackException e) {
System.err.println(e.getMessage());
}
}
}
}