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");         }     } }

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter15: Recursion
Section: Chapter Questions
Problem 6PE
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
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,