Question

Don't know how to write the code to check the braces and matching combinations(in Java) modify...

Don't know how to write the code to check the braces and matching combinations(in Java)

modify the lab below to include () as balanced pair
this means your program should check balances for {{(())}} or any pattern.

Once complete, write a program to test the method

Class name: BalancedBraces

Using recursion

Your program will check to see if braces are balanced.

For example {} is balanced { is not.

Your program will accept a string as such {}{{}{}{ and returns true or false if the braces are balanced

Ask the user to enter the brace combos.

Print balanced or unbalanced.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The solution of the given problem is below:

The screenshot of the ocode is below:

The screenshot of output  is below:

  

code :


import java.util.*;
import java.lang.*;
import java.io.*;

class BalancedBraces {
   public static void main (String[] args) {
       Scanner s=new Scanner(System.in);
       System.out.println("Enter braces as string:");
       String st=s.next();
       Stack<Character> stak=new Stack<>();
       int flag=1;
       for(int i=0;i<st.length();i++){
       char c=st.charAt(i);
       if(c=='(' || c=='[' || c=='{'){
       stak.push(c);
       }
       else if((!stak.empty()) &&
       ((c==')' && stak.peek()=='(')||
       (c==']' && stak.peek()=='[')||
       (c=='}' && stak.peek()=='{')))
       stak.pop();
       else{
       flag=0;
       break;
       }
       }
       if(flag==1 && stak.empty())
       System.out.println("balanced");
       else
       System.out.println("not balanced");
   }
}


Please upvote in case of any doubts comment me freely.

Add a comment
Know the answer?
Add Answer to:
Don't know how to write the code to check the braces and matching combinations(in Java) modify...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using recursion in java Your program will check to see if braces are balanced. For example...

    Using recursion in java Your program will check to see if braces are balanced. For example {} is balanced { is not. Your program will accept a string as such {}{{}{}{ and returns true or false if the braces are balanced Ask the user to enter the brace combos. Print balanced or unbalanced.

  • Java 8 Braces You are designing a compiler for a C++ program and need to check...

    Java 8 Braces You are designing a compiler for a C++ program and need to check that braces in any given file are balanced Braces in a string are considered to be balanced if the following criteria are met: All braces must be closed. Braces come in pairs of the form 0.0andl1. The left brace opens the pair, and the right one closes it In any set of nested braces, the braces between any pair must be closed For example,...

  • Please use Java. Write a non-recursive method that uses a stack to check whether its string...

    Please use Java. Write a non-recursive method that uses a stack to check whether its string parameter is a palindrome. Write a program to test and demo your method. Fibonacci Numbers Write a method that uses a stack to determine the desired Fibonacci number. Write a program to test and demo your method. Balancing Grouping Symbols Write a method that takes a string parameter and determines whether the string contains matching grouping symbols. Grouping symbols are parenthesis ( ), curly...

  • Write a program that will check a Java file for syntax errors. The program will ask...

    Write a program that will check a Java file for syntax errors. The program will ask for an input-file name and output-file name and will then copy all the code from the Java input file to the Java output file, but with the following changes: 1. Any syntax error found in the Java input file will be corrected: a. Missing semicolon b. Missing compound statements (curly braces) c. All comments have to start with // and end with a period....

  • Need answer in Python 3.8, with indentation and comments shown, please. 16.4* (Pattern matching) Write a...

    Need answer in Python 3.8, with indentation and comments shown, please. 16.4* (Pattern matching) Write a program that prompts the user to enter two strings and tests whether the second string is a substring in the first string. (Don't use the find method in the str class.) Analyze the time complexity of your algorithm. Here is a sample run of the program <output> Enter a string s1: Mississippi Enter a string s2: sip matched at index 6 <End Output>

  • The last 3 cases are tests .cpp files to test the code. The language of this...

    The last 3 cases are tests .cpp files to test the code. The language of this code must be C++ because that is the only I am familiar with. Soreland, a software company, is planning on releasing its first C++ compiler with the hopes of putting MiniSoft's Visual C++ out of business (that reminds me of another story). Consequently, Soreland has contracted with the Fruugle Corporation to design and build part of their compiler. Of course, since Fruugle gets all...

  • Task Algorithms: Pattern Matching (in java) Write a program that gets two strings from user, size...

    Task Algorithms: Pattern Matching (in java) Write a program that gets two strings from user, size and pattern, and checks if pattern exists inside size, if it exists then program returns index of first character of pattern inside size, otherwise it returns -1. The method should not use built-in methods such as indexOf , find, etc. Only charAt and length are allowed to use. Analyze the time complexity of your algorithm. Your solution is not allowed to be> = O...

  • Write a program that takes a file as input and checks whether or not the content...

    Write a program that takes a file as input and checks whether or not the content of the file is balanced. In the context of this assignment “balanced” means that your program will check to make sure that for each left bracket there is a closing right bracket. Examples:             [ ( ) ] { } à balanced.             [ ( ] ) { } à unbalanced. Here is the list of brackets/braces that program must support: (: left parentheses...

  • Please write in Java in Jgrasp check code before posting Please write in Java in Jgrasp...

    Please write in Java in Jgrasp check code before posting Please write in Java in Jgrasp check code before posting Please write in Java in Jgrasp check code before posting, will give thumbs up FOP- 2. Write a program that ciphers a plain text message using double transposition scheme and vice versa. [50 Marks) The program should accept five tuples of input: plain text, row, column, permutation of row and columns. An example of encrypted text for the plain text...

  • Write In JAVA: Part 1 The use of computers in education is referred to as computer-assisted...

    Write In JAVA: Part 1 The use of computers in education is referred to as computer-assisted instruction (CAI). Write a program that will help an elementary school student learn multiplication. Use a SecureRandom object to produce two positive one-digit integers (you will need to look up how to do this). The program should then prompt the user with a question, such as How much is 6 times 7? The student then inputs the answer. Next, the program checks the student’s...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT