Question

In: Computer Science

JAVA question A string may use more than one type of delimiter to bracket information into...

JAVA question

A string may use more than one type of delimiter to bracket information into "blocks". The primary ways to divide things up into block is by the braces { } , parentheses ( ), and brackets [ ] as delimiters.

  • A string is considered properly delimited if each right delimiter ] } or )( is matched with a preceding left delimiter { [ ( of the same type in such a way that either the resulting blocks of information are disjoint or one of them is contained nested within the other.  
  • Write a method that accepts a string in as a parameter and returns back a boolean value determining if the string is properly delimited.
  • Read the string char by char, ignoring all symbols but the [,{,(,},)]. Symbols.
    • if it is an open delimiter add it to the stack
    • if it is a close delimiter check two things
      • is the stack empty?
      • is the top of the stack the correct open type.
  • At the end of the string, if you haven't already returned an answer
    • Check that the stack is empty.
  • Use a single stack to check whether a string containing brackets is or is not properly delimited.
  • Test your method on the following strings:
  • String string1 = "[{}]";
    • good
  • String string2 = "[x{12345}xxx]xxx";
    • good
  • String string3 = "[1{2(3)4}5]";
    • good
  • String string4 = "[][](){}";
    • good
  • String string5 = "[[]}";
    • bad
  • String string6 = "[])";
    • bad
  • String string7 = "[]123{}123()123[{}]";
    • good
  • String string8 = "[[[[[]]]]]((((())))){{{{{{{{{{[]}}}}}}}}}}";
    • good
  • String string9 = "[[[[[]]]]]((((())))){{{{{{{{{{[]}}}}}}}}]}";
    • bad
  • String string10 = "[{({({({([[]])})})})}][{}]{}{{}}";
    • good
  • String string11 = "[{(";
    • bad
  • String math = "2+(3+5*(5+7-4)*[1+2+3+(6/5)])/(2*a)";
    • good

Solutions

Expert Solution

SOLUTION-
I have solve the problem in Java code with comments and screenshot for easy understanding :)

CODE-

//java code
import java.util.Scanner;
import java.util.Stack;


public class Main {
  
public static void main (String[] args) {

// createan object of Scanner class for user input
Scanner sc = new Scanner(System.in);
  
// get the string from the user
System.out.print("Enter String : ");
String exp = sc.next();

// create stack which stores the braces from the string that are enetered by the user
Stack<Character> stack = new Stack<>();

// now get each char from the string
for(int i=0; i<exp.length(); i++){
  
// read char by char using charAt() method
char ch = exp.charAt(i);
  
// if character is belongs to any braces then use stack. otherwise continue
if(ch == '{' || ch == '[' || ch == '(' || ch == ')' || ch == ']' || ch == '}'){
  
// check whether stack is empty then push the braces to the stack
if(stack.isEmpty()){
  
// character is added to the stack using push() method
stack.push(ch);
}else{
  
// When stack contain open braces and character of string contain same closed braces then pop that braces from the stack
if(ch == '}' && stack.peek() == '{' || exp.charAt(i) == ')' && stack.peek() == '(' || exp.charAt(i) == ']' && stack.peek() == '['){
  
// pop() same braces from the stack using pop() method
stack.pop();
}else{
  
// if there is no braces that same closed with top of the stack then add that character to the stack
stack.push(ch);
}
  
}
  
}
}
  
/*
After all the operation if stack is empty
then our string is good otherwise bad
*/
if(stack.isEmpty()){
  
// print in console that string is "good" because stack is empty
System.out.println("good");
}
else{
// print in console that string is "bad" because stack is not empty
System.out.println("bad");
}
  
}
}

SCREENSHOT-

Test few strings that are "good" and "bad".

1> When string is "good".

2> When string is "good".

3> When string is "good".

4> When string is "bad".

5> When string is "bad".

6> When string is "good".


IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK YOU!!!!!!!!----------


Related Solutions

WRITE IN JAVA PLEASE~ A string may use more than one type of delimiter to bracket...
WRITE IN JAVA PLEASE~ A string may use more than one type of delimiter to bracket information into "blocks". The primary ways to divide things up into block is by the braces { } , parentheses ( ), and brackets [ ] as delimiters. A string is considered properly delimited if each right delimiter ] } or )( is matched with a preceding left delimiter { [ ( of the same type in such a way that either the resulting...
JAVA / I KNOW THERE IS MORE THAN ONE QUESTION BUT THEY ARE SO EASY FO...
JAVA / I KNOW THERE IS MORE THAN ONE QUESTION BUT THEY ARE SO EASY FO YOU I NEED YOUR HELP PLEASE HELP ME. I WILL GIVE UPVOTE AND GOOD COMMENT THANK YOU SO MUCH !!! QUESTION 1 Consider the following program segment: int i = 2; int n = 18; while (i < n) {     if ((i % 2) == 0) i++; else n--; } System.out.println( i ); What is the value of n when the above program...
JAVA JAVA JAVA JAVA, How to determine if there is more than one number that appeared...
JAVA JAVA JAVA JAVA, How to determine if there is more than one number that appeared the X amount of times. (For example there are 3 numbers which occured 50 times so I want the output to show which 3 numbers occured 50 times) This is the code import java.util.Random; public class MostLeastOccurring { public static void main(String[] args) { Random random = new Random(); int x = 1000; int[] array = new int[x]; for (int i = 0; i...
2. There may be only one or there may be more than one correct response to...
2. There may be only one or there may be more than one correct response to this question. You need only mark any one of the possible correct responses to be marked correct. A - Touch the top of the electroscope with your finger B - Remove your finger from the top of the electroscope C - Touch the top of the electroscope with a glass rod that was just previously rubbed by silk D - Touch the top of...
Discuss why an organization may decide to target more than one market segment. If more than...
Discuss why an organization may decide to target more than one market segment. If more than one segment is targeted, what challenges do you think may exist?   
Question for Java String[] tokens = expression.split(" "); for (String token : tokens) { if (token.equals("+")...
Question for Java String[] tokens = expression.split(" "); for (String token : tokens) { if (token.equals("+") || token.equals("-")) { while (!ops.isEmpty() && (ops.peek() == '+' || ops.peek() == '-' || ops.peek() == '*' || ops.peek() == '/')) {    applyOp(values, ops); } What does the for (String tokens : token) mean? What are the different ways to write this for loop?
uestion 65 Note: this question may have more than one correct answer.    Under the revised MBCA,...
uestion 65 Note: this question may have more than one correct answer.    Under the revised MBCA, if there are business debts following a defective incorporation, liability for the debts will be imposed on which of the following? a. Shareholders who participated in management and policy decisions and knew of the defective incorporation. b. Managers who participated in decision-making while the corporation was operating. c. Promoters who knew of the defective incorporation. d. Shareholders who acted as if a corporation had...
A) Which of the following statements are true? Note that there may be more than one...
A) Which of the following statements are true? Note that there may be more than one correct answer; select all that are true. As the p-value increases, the evidence against the null hypothesis also increases. In hypothesis testing, if the p-value is sufficiently small, then the null hypothesis can be rejected in favour of the alternate hypothesis. If the null hypothesis is true, then the p-value will always be greater than 0.1. If the null hypothesis is false, then the...
As older adults live longer, they may have more than one chronic disease. Or, they may...
As older adults live longer, they may have more than one chronic disease. Or, they may have a health problem that can lead to another condition or injury if not properly managed. The older adult requires careful nutritional management to ensure proper care and improve or maintain quality of life. From the professional article you selected, describe the unique nutritional needs of this population
Disease: Leukemia Is there more than one type of disease? If so, what are they? What...
Disease: Leukemia Is there more than one type of disease? If so, what are they? What are the signs and symptoms of the disease? What are the possible treatments for the disease? What would you recommend to a patient who has been diagnosed with the disease? Can anything be done to prevent the disease?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT