please check my JAVA code. One of my test cases is wrong. The question is as stated in the image My answer: import java.util.*; public class BalanceSymbolChecker {     public static void main(String[] args) {         Scanner scn=new Scanner(System.in);         Stack stack=new Stack();         String str=scn.nextLine(); // take input         boolean ans=false;         int flag=0;          while(str.length()!=0) {             char ch=str.charAt(0);             str=str.substring(1);             if(stack.isEmpty()&&(ch==')'||ch==']'||ch=='}'||ch=='>')) {                 flag=1;                 break;             }             else if(ch=='('||ch=='['||ch=='{'||ch=='<') {                 stack.push(ch);             }             else if(ch==')') {                 char top=(char) stack.peek();                 if(top=='(') {                     stack.pop();                 }             }             else if(ch==']') {                 char top=(char) stack.peek();                 if(top=='[') {                     stack.pop();                 }             }             else if(ch=='}') {                 char top=(char) stack.peek();                 if(top=='{') {                     stack.pop();                 }             }             else if(ch=='>') {                 char top=(char) stack.peek();                 if(top=='<') {                     stack.pop();                 }             }         }         if(flag==1) {             ans=false;         }         else {             if(!stack.isEmpty()) {                 ans= false; //unbalanced             }             else {                 ans= true; //balanced             }         }         if(ans==true) {             System.out.println("Balanced");         }         else {             System.out.println("Unbalanced");         }     } }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
icon
Concept explainers
Question

please check my JAVA code. One of my test cases is wrong.

The question is as stated in the image

My answer:

import java.util.*;

public class BalanceSymbolChecker {
    public static void main(String[] args) {
        Scanner scn=new Scanner(System.in);
        Stack<Character> stack=new Stack<Character>();
        String str=scn.nextLine(); // take input

        boolean ans=false;
        int flag=0; 

        while(str.length()!=0) {
            char ch=str.charAt(0);
            str=str.substring(1);

            if(stack.isEmpty()&&(ch==')'||ch==']'||ch=='}'||ch=='>')) {
                flag=1;
                break;
            }
            else if(ch=='('||ch=='['||ch=='{'||ch=='<') {
                stack.push(ch);
            }
            else if(ch==')') {
                char top=(char) stack.peek();
                if(top=='(') {
                    stack.pop();
                }
            }
            else if(ch==']') {
                char top=(char) stack.peek();
                if(top=='[') {
                    stack.pop();
                }
            }
            else if(ch=='}') {
                char top=(char) stack.peek();
                if(top=='{') {
                    stack.pop();
                }
            }
            else if(ch=='>') {
                char top=(char) stack.peek();
                if(top=='<') {
                    stack.pop();
                }
            }
        }
        if(flag==1) {
            ans=false;
        }
        else {
            if(!stack.isEmpty()) {
                ans= false; //unbalanced
            }
            else {
                ans= true; //balanced
            }
        }

        if(ans==true) {
            System.out.println("Balanced");
        }
        else {
            System.out.println("Unbalanced");
        }
    }
}

 

Default
Standard 1
Standard 2
No Semicolon
Semicolon at the middle
OUTPUT
CODE REVIEW
COMMENT
0/0
25/25
25/25
0/25
25/25
X
Transcribed Image Text:Default Standard 1 Standard 2 No Semicolon Semicolon at the middle OUTPUT CODE REVIEW COMMENT 0/0 25/25 25/25 0/25 25/25 X
Problem Description
Compilers check your programs for syntax errors. Frequently, however, a lack of one symbol, such as a missing */ comment-ender, causes the compiler to produce numerous lines of diagnostics without identifying the real error. A useful tool
to help debug compiler error messages is a program that checks whether symbols are balanced.
In other words, every { must correspond to a }, every [ to a ], every (to a) and so on. However, simply counting the numbers of each symbol is insufficient. For example, the sequence [()] is legal, but the sequence [(]) is wrong. A
stack is useful here because we know that when a closing symbol such as ) is seen, it matches the most recently seen unclosed (.
Therefore, by placing an opening symbol on a stack, we can easily determine whether a closing symbol makes sense. The black box shown below represents the Balance Symbol Checker. If we give the input (4+ (2*3), the program will
display the error message. How this is done?
(4+ (23)
Error: Symbol ')' expected.
Challenge yourself to write a program to perform Balance Symbol Checker with implementation Stack.
Input
The input contains any symbols including alphabets or numbers. Each of them is separated into a single space. The input ends with the symbol semicolon. Input example:
(4+ ( 2 * 3 ) ;
(4+ (2* 3 )
Output
The output will be a word either 'Balanced' or 'Unbalanced'. The above input example will give the following output:
Unbalanced
ERROR: no END OF STRING in the expression
Transcribed Image Text:Problem Description Compilers check your programs for syntax errors. Frequently, however, a lack of one symbol, such as a missing */ comment-ender, causes the compiler to produce numerous lines of diagnostics without identifying the real error. A useful tool to help debug compiler error messages is a program that checks whether symbols are balanced. In other words, every { must correspond to a }, every [ to a ], every (to a) and so on. However, simply counting the numbers of each symbol is insufficient. For example, the sequence [()] is legal, but the sequence [(]) is wrong. A stack is useful here because we know that when a closing symbol such as ) is seen, it matches the most recently seen unclosed (. Therefore, by placing an opening symbol on a stack, we can easily determine whether a closing symbol makes sense. The black box shown below represents the Balance Symbol Checker. If we give the input (4+ (2*3), the program will display the error message. How this is done? (4+ (23) Error: Symbol ')' expected. Challenge yourself to write a program to perform Balance Symbol Checker with implementation Stack. Input The input contains any symbols including alphabets or numbers. Each of them is separated into a single space. The input ends with the symbol semicolon. Input example: (4+ ( 2 * 3 ) ; (4+ (2* 3 ) Output The output will be a word either 'Balanced' or 'Unbalanced'. The above input example will give the following output: Unbalanced ERROR: no END OF STRING in the expression
Expert Solution
steps

Step by step

Solved in 3 steps with 3 images

Blurred answer
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

Now the last test case is wrong. Can u help me

Default
Standard 1
Standard 2
No Semicolon
Semicolon at the middle
0/0
25/25
25/25
25/25
<
0/25 X
Transcribed Image Text:Default Standard 1 Standard 2 No Semicolon Semicolon at the middle 0/0 25/25 25/25 25/25 < 0/25 X
Solution
Bartleby Expert
SEE SOLUTION
Knowledge Booster
Depth First Search
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education