| | |
help me guys! i have a problem on how to use Stack! :((
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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.
I don't accept change; I don't deserve to live.
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
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

convert the String to array of character using

create a Stack object eg
if there is an occurrence of character
, it will be pushed onto the stack object using
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
import java.util.Stack; read all the mathimatical equation into a String variable say convert the String to array of character using
mathStString.toCharArray(); assign this to a variable saycreate a Stack object eg
Stack stack= new Stack(); use a for loop to scan through mathCharArrayif there is an occurrence of character
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
•
•
Join Date: Dec 2008
Posts: 2
Reputation:
Solved Threads: 0
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..
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..
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.
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.
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
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
![]() |
Similar Threads
- problem with understanding buffers (Assembly)
- STL Help .. Please ... An intresting problem !!! (C++)
- SUB the contents of two arrays and move the result in a third array (Assembly)
- problem with output values (Assembly)
- *The* Problem With JSP...? (JSP)
- HULLO guys.. (Windows NT / 2000 / XP)
Other Threads in the Java Forum
- Previous Thread: youngest person to pass the SCJA certification?
- Next Thread: Help with Sorts
Views: 563 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for Java
add android api apple applet application arguments array arrays automation bank binary bluetooth chat chooser class classes client code component converter database digit draw eclipse equation error event exception file fractal functiontesting game givemetehcodez graphics gui health helpwithhomework html hyper ide idea image infinite input int integer j2me java javame javaprojects jmf jni jpanel julia linux list loop main map method methods mobile myregfun netbeans newbie nonstatic number object oracle pattern pearl print problem program programming project recursion scanner screen scrollbar server set size sms socket sort sorting spamblocker sql sqlserver string superclass swing test thread threads time tree windows




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

