write a java program that checks if parenthesis and brackets are balanced in an arithmetic expression.

help me!! :(( i dunno how to solve that problem.. is it the same as the calculator java code? the concept, i mean.. :( help me guys..

It's pretty simple.

  • Parse the expression entered and iterate over it on a character by character basis.
  • If the given character is not a parenthesis or bracket, move over to the next one. If it is an opening parenthesis or bracket, push it on to the stack and move on to the next one.
  • If the given character is a closing parenthesis or bracket, check its compatibility by peeking at character at the top of stack. If it matches, pop the top of the stack and move on to the next character. If it doesn't, throw an error.
  • If the stack contains extra characters even after the entire expression has been parsed, throw an error.

Though I have ignored the details out there, I am pretty sure you can take it from here. Implement the above logic and post the entire code if you have any problems.

Am sorry I may not be of full help now cause am also new i JAVA but let me give you the algorithm that came to my mind on this import java.util.Stack; read all the mathimatical equation into a String variable say [TEX]mathString[/TEX]
convert the String to array of character using mathStString.toCharArray(); assign this to a variable say
[TEX]mathCharArray[/TEX]
create a Stack object eg Stack stack= new Stack(); use a for loop to scan through mathCharArray
if there is an occurrence of character [TEX]'('[/TEX], it will be pushed onto the stack object using stack.push(new Character('(')); if the char is ')' checkk
if the stack is not empty,
throw an Exception that there is a closing parenthesis with out a corresponding opening parenthesis.
otherwise
remove the character from the stack

after completing the loop cheek if stack is empty
if stack is not empty
throw an exception that there is an unclosed parenthesis

^ Huh?


What S.o.S said is correct, and also, if I'm not mistaken, is what Eclipse and a lot of other IDE's and editors use to 'check' whether or not there is a syntax error (w/ parens and brackets).

import java.util.EmptyStackException;
import java.util.Stack;

public class Balance {
    Braces br = new Braces();

        public boolean isBalanced(String string){
             Stack<Character> stack = new Stack<Character>();
             boolean bal = true;
             int i = 0;

             while(bal && (i < string.length())){
                char ch = string.charAt(i);
                i++;
                if(br.open_bracket(ch)){
                   stack.push(ch); 
                }
                else if(br.close_bracket(ch)){
                   try{
                      stack.pop();
                   }catch(EmptyStackException exception){
                         bal = false;
                   }
                }

              }
             if(bal && stack.isEmpty()){
                return true;
             }
             else{
                return false;
             }
          }
        }

---------------- is this ok? another problem, i dunno how to code if there are no parentheses..

Edited 3 Years Ago by Dani: Formatting fixed

1. Use code tags to post code.
2. Instead of asking of us whether your code is ok or not you can check it yourself, if it does what it is intended to do you know what it is, whether it doesn't you still know what it is. Why do you need our approval.

@~s.o.s~ said : If the given character is not a parenthesis or bracket, move over to the next one

3. doesn't this pretty much explain what you got o do when there are no paranthesis at all ? You just keep on moving over all the characters till the end of the expression.

Comments
Well said
This article has been dead for over six months. Start a new discussion instead.