Write a program to identify mismatched brackets This happens

Write a program to identify mismatched brackets. This happens when there are too many opening brackets (\'{\', \'[\', \'(\' and \'<\') and not enough closing brackets (\'}\', \']\', \')\' and \'>\'), or vice versa; or when an opening bracket of one type corresponds to a closing bracket of another type, for example if a \'(\' is closed by a \']\'. Write a program that takes a string as input. It should then scan the string, checking that all brackets match. If it encounters an opening bracket that does not match its corresponding closing bracket, it should print a message to that effect and then terminate any further checking as further errors will be ambiguous. If, at the end of the scan, there remain unclosed brackets or excess closing brackets, a message should be printed to that effect. Hint: this problem is best solved using a stack, by popping brackets from the end off and storing them in another stack until an opening bracket is found. You may write your own stack implementation, or use the built-in java.util.Stack class. It may be helpful to write other methods such as ones that check whether a bracket is an opening or closing bracket and ones that return the corresponding bracket to a given bracket. However, these are not essential.

Sample Input/Output Enter a string to test:

( < [ { } ( { > ) ] >

error: \'>\' does not match with \'{\'.

Enter a string to test: { ( [ ] ) { ( ) ( ) } }

The string is correct! There are no mismatched brackets.

Enter a string to test: { ( [ ] ) { ( ) ( ) }

error at end: opening bracket \'{\' remains unclosed.

Enter a string to test: ( [ ] ) < > ] }

error at end: the close bracket \']\' does not have a corresponding opening bracket.

error at end: the close bracket \'}\' does not have a corresponding opening bracket.

Solution

Hi,

with little modification for correct string .Copy it again to your local.

===

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapp;
import java.io.*; // for I/O
/**
*
* @author welcome
*/

public class BracketsApp {
public static void main(String[] args) throws IOException {
String input;
while (true) {
System.out.print(\"Enter string containing delimiters: \");
System.out.flush();
input = getString(); // read a string from kbd
if (input.equals(\"\")) // quit if [Enter]
break;
// make a BracketChecker
BracketChecker theChecker = new BracketChecker(input);
theChecker.check(); // check brackets
} // end while
} // end main()
// --------------------------------------------------------------

public static String getString() throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String s = br.readLine();
return s;
}
// --------------------------------------------------------------
}
// end class BracketsApp
class StackX {
private int maxSize;
private char[] stackArray;
private int top;

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

// --------------------------------------------------------------
public void push(char j) // put item on top of stack
{
stackArray[++top] = j;
}

// --------------------------------------------------------------
public char pop() // take item from top of stack
{
return stackArray[top--];
}

// --------------------------------------------------------------
public char peek() // peek at top of stack
{
return stackArray[top];
}

// --------------------------------------------------------------
public boolean isEmpty() // true if stack is empty
{
return (top == -1);
}
// --------------------------------------------------------------
} // end class StackX

class BracketChecker {
private String input; // input string
// --------------------------------------------------------------

public BracketChecker(String in) // constructor
{
input = in;
}

// --------------------------------------------------------------
public void check() {
boolean gotError=false;
int stackSize = input.length(); // get max stack size
StackX theStack = new StackX(stackSize); // make stack

for (int j = 0; j < input.length(); j++) // get chars in turn
{
char ch = input.charAt(j); // get char
switch (ch) {
case \'{\': // opening symbols
case \'[\':
case \'(\':
case \'<\':
theStack.push(ch); // push them
break;

case \'}\': // closing symbols
case \']\':
case \')\':
case \'>\':
if (!theStack.isEmpty()) // if stack not empty,
{
char chx = theStack.pop(); // pop and check
if ((ch == \'}\' && chx != \'{\') || (ch == \']\' && chx != \'[\') || (ch == \')\' && chx != \'(\')||(ch == \'>\' && chx != \'<\')){
System.out.println(\"error:\'\"+ch+\"\' does not match with \'\"+chx+\"\'.\");
gotError=true;

}
} else{
// prematurely empty
System.out.println(\"error at end:the close bracket \'\"+ch+\"\' does not have a corresponding opening bracket.\");
gotError=true;

break;
}
default: // no action on other characters
  
break;
} // end switch
} // end for
// at this point, all characters have been processed;
if (!theStack.isEmpty()){
System.out.println(\"error at end : opening bracket \'\"+theStack.peek()+\"\' remains unclosed.\");
}else if(!gotError) {
System.out.println(\"The string is correct! There are no mismatched brackets.\");
}
  
}

  

}

  

===

run:
Enter string containing delimiters: ( < [ { } ( { > ) ] >
error:\'>\' does not match with \'{\'.
error at end : opening bracket \'(\' remains unclosed.
Enter string containing delimiters: { ( [ ] ) { ( ) ( ) } }
The string is correct! There are no mismatched brackets.
Enter string containing delimiters: { ( [ ] ) { ( ) ( ) }
error at end : opening bracket \'{\' remains unclosed.
Enter string containing delimiters: ( [ ] ) < > ] }
error at end:the close bracket \']\' does not have a corresponding opening bracket.
error at end:the close bracket \'}\' does not have a corresponding opening bracket.
Enter string containing delimiters:

Write a program to identify mismatched brackets. This happens when there are too many opening brackets (\'{\', \'[\', \'(\' and \'<\') and not enough closing
Write a program to identify mismatched brackets. This happens when there are too many opening brackets (\'{\', \'[\', \'(\' and \'<\') and not enough closing
Write a program to identify mismatched brackets. This happens when there are too many opening brackets (\'{\', \'[\', \'(\' and \'<\') and not enough closing
Write a program to identify mismatched brackets. This happens when there are too many opening brackets (\'{\', \'[\', \'(\' and \'<\') and not enough closing

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site