Question

In: Computer Science

Write a Java application that checks a string for correct parenthesization. The input comes from the...

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 must include following:

  • OK
  • Opener/closer mismatch
  • Missing closer
  • Missing opener
  • Unexpected symbol

The name of your class should be ParenChecker.

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:

Missing closer

etc...

Solutions

Expert Solution

Code:

import java.util.Scanner;
class ParenChecker{
   public static void main(String args[]){
       Scanner s=new Scanner(System.in);
       System.out.print("enter input:");       //reading input.
       String k=s.next();   
       int top=-1,i,c=0;
       char j;
       char a[]=new char[100];
       for(i=0;i<k.length();i++){
           j=k.charAt(i);
           if(j=='(' || j=='{' || j=='['){
               a[++top]=j;                   //push into the stack.
          
           }
           else if(top!=-1 && (j=='}' || j==']' || j==')' ) ){
               if(j==']' && a[top]=='[')
                   top=top-1;
               else if(j=='}' && a[top]=='{')
                   top=top-1;
               else if(j==')' && a[top]=='(')
                   top=top-1;
               else{
                   System.out.println("Opener/Closer mismatch");
                   c=1;
                   break;
               }

           }
           else{
               System.out.println("Unexpected symbol");        //if unexpected symbol occurs other than brackets.

               break;
          
           }

       }
       if(top==-1)
           System.out.println("OK");       //printing ok if parenthesis is correct.
       else if(top!=-1 && c!=1){
           j=k.charAt(0);
           if(j=='(' || j=='{' || j=='[')
               System.out.println("Missing Closer");     
           if(j==')' || j=='}' || j==']')
               System.out.println("Missing Opener");
       }   
   }
}

Output 1:

Output 2:


Related Solutions

Write a Java application with a JavaFXGUI that takes a String input by the user and...
Write a Java application with a JavaFXGUI that takes a String input by the user and shows whether the string contains all 26 letters of the (English version of the Latin) alphabet. For example, "Pack my box with five dozen liquor jugs" contains all 26 letters, but "The quick frown box jumps over the hazy log" does not contain a d. It does not matter whether one or more letters appear more than once. The GUI needs, at minimum, a...
Write a Java application with a JavaFXGUI that takes a String input by the user and...
Write a Java application with a JavaFXGUI that takes a String input by the user and shows whether the string contains all 26 letters of the (English version of the Latin) alphabet. For example, "Pack my box with five dozen liquor jugs" contains all 26 letters, but "The quick frown box jumps over the hazy log" does not contain a d. It does not matter whether one or more letters appear more than once. The GUI needs, at minimum, a...
Write a JAVA program that reads in a string from standard input and determines the following:...
Write a JAVA program that reads in a string from standard input and determines the following: - How many vowels are in the string (FOR THE PURPOSE OF THIS PROGRAM 'Y' is NOT considered a vowel)? - How many upper case characters are in the string? - How many digits are in the string? - How many white space characters are in the string? - Modify the program to indicate which vowel occurs the most. In the case of a...
Write a driver to get a String input from keyboard and if the input string has...
Write a driver to get a String input from keyboard and if the input string has less than 10 characters, throw a StringTooShortException. public class StringTooShortException extends Exception {     //-----------------------------------------------------------------     // Sets up the exception object with a particular message.     //-----------------------------------------------------------------     public StringTooShortException()     {         super("String does not have enough characters");     } }
Write an X86-series assembly language program that checks whether input string is palindrome or not. A...
Write an X86-series assembly language program that checks whether input string is palindrome or not. A palindrome is a word, number, phrase or any other sequence which reads the same backward as forward e.g. madam, racecar. Sample Execution: Please enter a String: redivider The string is a palindrome Another Sample Execution: Please enter a String: abracadabra The string is not a palindrome
Write a Java program that prompts the user to input a string and prints whether it...
Write a Java program that prompts the user to input a string and prints whether it is a palindrome. A palindrome is a string which reads the same backward as forward, such as Madam (disregarding punctuation and the distinction between uppercase and lowercase letters). The program must use the stack data structure. The program must include the following classes: The StackX class (or you can use the Java Stack class). The Palindrome class which must contain a method named palindrome()...
Write a Java method that takes an array of char and a String as input parameters...
Write a Java method that takes an array of char and a String as input parameters and and returns an boolean. The method returns true if we can find the input string inside the array by starting at any position of the array and reading either forwards or backwards.
Write an interactive Java class which accepts an input argument when the application is executed from...
Write an interactive Java class which accepts an input argument when the application is executed from the command-line. Accept input from the user and compare the value entered to the command-line argument value. If the strings do not equal, display "INVALID VALUE! TRY AGAIN!", otherwise display "PERMISSION GRANTED!" and exit the program.
JAVA 1. Write an application that inputs a telephone number as a string in the form...
JAVA 1. Write an application that inputs a telephone number as a string in the form (555) 555-5555. The application should use String method split to extract the area code as a token, the first three digits of the phone number as a token and the last four digits of the phone number as a token. The seven digits of the phone number should be concatenated into one string. Both the area code and the phone number should be printed....
Write a Java console application that reads a string for a date in the U.S. format...
Write a Java console application that reads a string for a date in the U.S. format MM/DD/YYYY and displays it in the European format DD.MM.YYYY  For example, if the input is 02/08/2017, your program should output 08.02.2017
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT