Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Having trouble finishing this program. In JAVA, I need to write a brackets method called, check(). This method will be contained in a class called

Having trouble finishing this program. In JAVA, I need to write a brackets method called, check(). This method will be contained in a class called BracketChecker. This method will check that for each left delimiter, {, [, (, a right delimiter exists, ), ], }, and in the correct order.

Use a stack to insure that these delimiters are paired correctly. The user will enter the delimiters within the main() method and these delimiters will be passed to the BracketChecker class through the constructor.

Here is an example of output when I enter delimiters that don't match that shows my problem:

Enter string containing delimiters: {[ Error: missing right delimiter

The delimiters are all correct. There were no errors

Enter string containing delimiters:

As you can see, the error message was correct, but then the message that's supposed to be displayed when the delimiters match is displayed and the program keeps running. If I input matching delimiters, this is the output:

Enter string containing delimiters: {[]} Enter string containing delimiters:

As you can see, it doesn't display an error message, but it also doesn't display the correct message, and the program keeps running.

The main() method, the StackX class, and the BracketChecker class are given below. Do not make any modifications to the main() method or the StackX class. Use it as given.

public static void main(String[] args)

{

boolean flag = true;

String input;

Scanner keyboard = new Scanner(System.in);

while(flag)

{

System.out.print("Enter string containing delimiters: ");

input = keyboard.nextLine();

if( input.equals("") )

flag = false;

else

{

BracketChecker theChecker = new BracketChecker(input);

if (!theChecker.check())

System.out.println(" The delimiters are all correct. There were no errors ");

}

}

}

public class StackX { private int maxSize; private long[] stackArray; private int top;

public StackX(int s) { maxSize = s; stackArray = new long[maxSize]; top = -1; }

public void push(long j) { stackArray[++top] = j; }

public long pop() { return stackArray[top--]; }

public long peek() { return stackArray[top]; }

public boolean isEmpty() { return (top == -1); }

public boolean isFull() { return (top == maxSize-1); } }

public class BracketChecker { private String input; private StackX theStack;

public BracketChecker (String in) { input = in; int stackSize = input.length (); theStack = new StackX (stackSize); }

public boolean check () { for (int i = 0; i < input.length (); i++) { char ch = input.charAt (i); switch (ch) { case '{': case '[': case '(': theStack.push (ch); break; case '}': case ']': case ')': if (!theStack.isEmpty ()) { char chx = (char) theStack.pop (); if ((ch == '}' && chx != '{') || (ch == ']' && chx != '[') || (ch == ')' && chx != '(')) { System.out.println ("Error: " + ch + " at " + i); return false; } } else { System.out.println ("Error: " + ch + " at " + i); return false; } break; default: break; } } if (!theStack.isEmpty ()) { System.out.println ("Error: missing right delimiter"); return false; } return true; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2014 Nancy France September 15 19 2014 Proceedings Part I Lnai 8724

Authors: Toon Calders ,Floriana Esposito ,Eyke Hullermeier ,Rosa Meo

2014th Edition

3662448475, 978-3662448472

More Books

Students also viewed these Databases questions