Write a java program called Mismatch.java(that takes a string as input) to ident
ID: 670664 • 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
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.