MatchChecker class The MatchChecker class is a utility class that will be used t
ID: 3641545 • Letter: M
Question
MatchChecker class
The MatchChecker class is a utility class that will be used to check if parentheses (), brackets [], and braces {} are matched in a given string. The MatchChecker class object will never be instantiated. It must have the following method:
public static String matchChecker(String lineToCheck)
The matchChecker method's argument will be a string that can contain parenthesis, brackets, and braces. Other characters or numbers can appears before/after/between them.
If all of them are matched, then the method should return the string:
"Everything is matched!"
If there is a closing parenthesis, bracket, or brace that does not have its corresponding opening parenthesis, bracket, or brace,
then for the first such character, return a string with its position such as:
") at the position 15 does not match."
"] at the position 12 does not match."
OR
"} at the position 28 does not match."
The first character of the string is seen as the position 0.
If there is no matching closing parenthesis, bracket, and brace when you reach the end of the string after checking each character,
return a string notifying the last opening parenthesis, bracket, or brace that did not have its matching closing one as:
") is missing."
"] is missing."
OR
"} is missing."
Requirement:
You need to implement this method using an object of Stack in java.util package.
You can use the string "A" as a token for parentheses, "B" as a token for brackets, "C" as a token for braces.
When an opening parenthesis is read, push the token "A", and when a closing parenthesis is read, pop the token "A" if available.
A similar operation can be done for others. Only if the stack is empty when you finish reading all characters of the string, you can confirm that everything was matched. Otherwise, the method needs to return an appropriate message stated above.
there is two classes .
class1: is Assignment
Class2 : is MatchChecker
so the Assignment class is
import java.io.*;
public class Assignment
{ public static void main (String[] args)
throws IOException
{
char input1;
String inputInfo;
String line = new String();
printMenu();
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader stdin = new BufferedReader(isr);
do // will ask for user input
{
System.out.println("What action would you like to perform?");
line = stdin.readLine();
input1 = line.charAt(0);
input1 = Character.toUpperCase(input1);
if (line.length() == 1)
{ // matches one of the case statements
switch (input1)
{
case 'E': //Enter String
System.out.print("Please enter a string. ");
inputInfo = stdin.readLine().trim();
System.out.println(MatchChecker.matchChecker(inputInfo));
break;
case 'Q': //Quit
break;
case '?': //Display Menu
printMenu();
break;
default:
System.out.print("Unknown action ");
break;
}
}
else
{
System.out.print("Unknown action ");
}
} while (input1 != 'Q' || line.length() != 1);
}
/** The method printMenu displays the menu to a user**/
public static void printMenu()
{
System.out.print("Choice Action " + "------ ------ " + "E Enter String " +"Q Quit " + " Display Help "); }}