A Simple Parenthesis Checker (ParenChecker)
Write a Java application that checks a string for correct parenthesization. The input comes from the keyboard and should (but might not) consist solely of the delmiters (, ), [, ], {, and } separated by whitespace.
The output of the program is one of the following:
The name of your class should be ParenChecker.
The reasonable thing to do here is to use a stack — the Java Collection Hierarchy has one.
For example, if the input was:
( [ { } ] )
the program should produce the following output:
OK
and if the input was:
( [ ) ]
the program should produce the output:
Opener/closer mismatch
Code Screenshots (ParenChecker.Java):
Sample Output 1:
Sample Output 2:
Code to Copy (ParenChecker.Java):
//Import the
//required packages.
import java.util.*;
//Define the class ParenChecker.
public class ParenChecker {
//Define the
//main() method.
public static void main(String args[])
{
//Define an object
//of the Scanner class.
Scanner input = new Scanner(System.in);
//Define a Stack to
//store the open parenthesis.
Stack<Character> paren_stack = new Stack<Character>();
//Define the set of valid
//and closed parenthesis.
char [] valid_open_paren = {'(', '[', '{'};
char [] valid_close_paren = {')', ']', '}'};
//Define a string
//to store the input.
String input_string;
System.out.print("Enter the input: ");
//Read and
//store the input.
input_string = input.nextLine();
int i = 0;
int is_valid = 1;
int open_count = 0;
int close_count = 0;
//Run the loop to check the
//validity of the input string.
while(i<input_string.length())
{
//Read the next character
//from the input string.
char curr_char = input_string.charAt(i);
i++;
//Define a flag variable to
//check the type of parenthesis
//found in the input string.
int found = 0;
int paren_num = -1;
//Run the loop to check if
//the current parenthesis
//is a valid one or not.
for(int k=0; k < valid_open_paren.length; k++)
{
//If the current character
//is a open parenthesis then,
//set the found variable to 1
//and break out of the for loop.
if(curr_char == valid_open_paren[k])
{
found = 1;
open_count++;
break;
}
//If the current character is a
//closing parenthesis then set
//the found flag to 2 and
//break out of the for loop.
if(curr_char == valid_close_paren[k])
{
found = 2;
close_count++;
paren_num = k;
break;
}
}
//If the value of flag
//found is 0 then,
//set the is_valid flag to 5
//and break out of the loop.
if(found == 0)
{
is_valid = 5;
break;
}
//Otherwise, if the current
//character is an open parenthesis
//then, push the
//parenthesis onto the stack.
else if(found == 1)
{
paren_stack.push(curr_char);
}
//Otherwise, pop an
//element from the stack
else
{
//If the stack is not empty then,
//pop a parenthesis from the stack.
if(!paren_stack.empty())
{
char stack_top = paren_stack.pop();
//If the parenthesis is not a
//correct match then set the
//flag is_valid as 2 and break
//out of the loop.
if(stack_top != valid_open_paren[paren_num])
{
is_valid = 2;
break;
}
}
//Otherwise, if the stack
//is empty then, set the flag
//is_valid to 4 and break
//out of the loop.
else
{
is_valid = 4;
break;
}
}
if(i < input_string.length())
{
char white_space;
white_space = input_string.charAt(i);
if(white_space != ' ')
{
is_valid = 5;
break;
}
i++;
}
}
if(is_valid == 1)
{
if(open_count > close_count)
{
is_valid = 3;
}
else if(close_count > open_count)
{
is_valid = 4;
}
}
//Check the flag
//is_valid and display
//the output accordingly.
switch(is_valid)
{
case 1:
System.out.println("OK");
break;
case 2:
System.out.println("Opener/closer mismatch");
break;
case 3:
System.out.println("Missing closer");
break;
case 4:
System.out.println("Missing opener");
break;
case 5:
System.out.println("Unexpected symbol");
break;
default:
}
//Close the
//Scanner object.
input.close();
}
}
Get Answers For Free
Most questions answered within 1 hours.