Java Online Compiler
Example: Remove Brackets by Iteration in Java
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// Remove Brackets by Iteration import java.util.Scanner; // Main class containing the entry point of the program public class Main { public static void main(String[] args) { // Step 1: Define the algebraic expression String expressionWithBrackets = "(10 + [5 * (x - y)] / {2})"; System.out.println("Original Expression: " + expressionWithBrackets); // Step 2: Initialize a StringBuilder to efficiently build the new string StringBuilder sb = new StringBuilder(); // Step 3: Iterate through each character of the expression for (char c : expressionWithBrackets.toCharArray()) { // Step 4: Check if the character is a bracket if (c != '(' && c != ')' && c != '[' && c != ']' && c != '{' && c != '}') { // Step 5: If it's not a bracket, append it to the StringBuilder sb.append(c); } } // Step 6: Convert the StringBuilder content to a String String expressionWithoutBrackets = sb.toString(); // Step 7: Print the modified expression System.out.println("Expression without Brackets (Iteration): " + expressionWithoutBrackets); // Example with another expression String anotherExpression = "{2 * (a + b)} - [c / (d + e)]"; System.out.println("\nOriginal Expression: " + anotherExpression); StringBuilder sb2 = new StringBuilder(); for (char c : anotherExpression.toCharArray()) { if (c != '(' && c != ')' && c != '[' && c != ']' && c != '{' && c != '}') { sb2.append(c); } } System.out.println("Expression without Brackets (Iteration): " + sb2.toString()); } }
Output
Clear
ADVERTISEMENTS