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

Write a java program called Mismatch.java(that takes a string as input) to ident

ID: 671330 • Letter: W

Question

Write a java program called Mismatch.java(that takes a string as input) 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 ']'. It should 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.

By using a stack-this problem may be solved easier, by popping brackets from the end off and storing them in another stack until an opening bracket is found. 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.

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.

Explanation / Answer

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote