Balanced Multiple Delimiters modified from program challenge 18-15 (p.1100). The
ID: 3539840 • Letter: B
Question
Balanced Multiple Delimiters modified from program challenge 18-15 (p.1100).
The purpose of this assignment is to detect whether the delimiters are balanced, i.e. the counting up and down of the opening and closing (of a specific delimiter) is balanced. This assignment is not intended as a full exercise for a parser. The delimiters have no semantic meaning as in a programming language.
A string may use more than one type of delimiter to bracket information into blocks. For example, A string may use braces { }, parentheses ( ), angle brackets <> and square brackets [ ] as delimiters. A string is properly delimited if each right delimiter is matched with a preceding left delimiter of the same type in such a way that either the resulting blocks of information are disjoint, or one of them is completely nested within the other.
Write a program that uses multiple stacks to check whether a file containing braces, parentheses, and brackets is properly delimited or not. Input the test pattern from the test.txt file to check the delimiters inside are balanced or not. Report any unbalanced delimiter types, with the indication on which of the opening or closing delimiter is unbalanced.
For example,
{ while(a==(3+b)){ b = c+d; if (b == c){cout << b;} else cout << c;}}}
The above code (from the selected input file) is not closing brace balanced, and not opening angle bracket balanced.
Provide a simple menu, so the user can apply the balanced test to different files without having to invoke the program for different test.
Explanation / Answer
import java.util.Stack;
import java.io.*;
import java.util.*;
class BracketChecker {
private String input;
public BracketChecker(String in) {
input = in;
}
public void check()
{
Stack theStack = new Stack<Character>();
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().toString().charAt(0);
if ((ch == '}' && chx != '{') || (ch == ']' && chx != '[') || (ch == ')' && chx != '('))
{
System.out.println("Error: " + ch + " at position:" + j);
return;
}
}
else
{
System.out.println("Error: " + ch + " at position:" + j);
return;
}
default:
break;
}
}
if (!theStack.isEmpty())
{
System.out.println("Error: missing right delimiter");
return;
}
System.out.println("No error..completely balanced");
}
}
public class del
{
public static void main(String[] args) throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int flag=1;
while(flag==1)
{
System.out.println("Enter file name:");
String fn=br.readLine();
String content = new Scanner(new File(fn)).useDelimiter("\Z").next();
BracketChecker theChecker = new BracketChecker(content);
theChecker.check();
System.out.println("Press 1 to continue with another file name:");
flag=Integer.parseInt(br.readLine());
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.