Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

By Using Java Write a program that examines a string of characters and determine

ID: 3836893 • Letter: B

Question

By Using Java

Write a program that examines a string of characters and determines if all opening bracket characters such as (, [, or { are matched with their correct closing characters. Use a stack to do this. The basic algorithm is as follows:

1) Read the string, character by character.

2) If the character is an opening bracket character, push its equivalent closing character onto the stack.

3) If the character is a closing bracket character, pop the stack, and see if the character you popped matches the current character. If not, the brackets in the string are not paired correctly.

4) If the character is not a bracket character, ignore it.

5) If you get through the whole string without failing, check to see if the stack is empty. If it is, the brackets are all matched correctly. If not, the brackets were not matched correctly.

6) Your program should print a message back to the user if the brackets are correctly matched or not

Test your program on some small strings. Once it is working properly, it should work regardless of the string. Try using it on an entire Java source code file.

Explanation / Answer

Hi, Please find my implementation.

Please let me know in case of any issue.

import java.util.EmptyStackException;

import java.util.Stack;

/** Class to check for balanced parentheses.

* @author Pravesh

**/

public class ParenChecker {

   // Constants

   /** Set of opening parenthesis characters. */

   private static final String OPEN = "([{";

   /** Set of closing parenthesis characters, matches OPEN. */

   private static final String CLOSE = ")]}";

   public static boolean isBalanced(String expression) {

       // Create an empty stack.

       Stack<Character> s = new Stack<Character>();

       boolean balanced = true;

       try {

           int index = 0;

           while (balanced && index < expression.length()) {

               char nextCh = expression.charAt(index);

               if (isOpen(nextCh)) {

                   s.push(nextCh);

               } else if (isClose(nextCh)) {

                   char topCh = (char) s.pop();

                   balanced =

                           OPEN.indexOf(topCh) == CLOSE.indexOf(nextCh);

               }

               index++;

           }

       } catch (EmptyStackException ex) {

           balanced = false;

       }

       return (balanced && s.empty());

   }

   /**

   * Method to determine whether a character is one of the

   * opening parentheses.

   * @param ch Character to be tested

   * @return true if ch is one of the opening parentheses

   */

   private static boolean isOpen(char ch) {

       return OPEN.indexOf(ch) > -1;

   }

   /**

   * Method to determine whether a character is one of the

   * closing parentheses.

   * @param ch Character to be tested

   * @return true if ch is one of the closing parentheses

   */

   private static boolean isClose(char ch) {

       return CLOSE.indexOf(ch) > -1;

   }

  

   public static void main(String[] args) {

      

       String str = "{[public()]exmpty}";

       if(isBalanced(str))

           System.out.println("Balanced");

       else

           System.out.println("Not Balanced");

   }

}