943,901 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 885
  • Java RSS
Dec 20th, 2008
0

help me guys! i have a problem on how to use Stack! :((

Expand Post »
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..
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bhob is offline Offline
8 posts
since Dec 2008
Dec 20th, 2008
0

Re: help me guys! i have a problem on how to use Stack! :((

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.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Dec 20th, 2008
0

Re: help me guys! i have a problem on how to use Stack! :((

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 mathString
convert the String to array of character using
mathStString.toCharArray(); assign this to a variable say
mathCharArray
create a Stack object eg
Stack stack= new Stack(); use a for loop to scan through mathCharArray
if there is an occurrence of character '(', 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
awo
Reputation Points: 10
Solved Threads: 1
Newbie Poster
awo is offline Offline
21 posts
since Oct 2007
Dec 22nd, 2008
0

Re: help me guys! i have a problem on how to use Stack! :((

^ 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).
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Jan 3rd, 2009
0

Re: help me guys! i have a problem on how to use Stack! :((

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..
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bhob is offline Offline
8 posts
since Dec 2008
Jan 3rd, 2009
1

Re: help me guys! i have a problem on how to use Stack! :((

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.
Quote ...
@~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.
Reputation Points: 485
Solved Threads: 89
Posting Shark
verruckt24 is offline Offline
944 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: please someone help me
Next Thread in Java Forum Timeline: Help with Sorts





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC