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
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.
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
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.