Gtown :
I made a program that deals with asking the user to input a equation such as { x + (y - 2) * 1} + [ a + b] and checking if every parenthesis and bracket etc closes. If my method spots that it closes it will print: Expression: { x + (y - 2) * 1} + [ a + b] -> is balanced. My problem is my method seems to default to unbalanced everytime so I suspect its a problem within the isBalanced method.Could anyone just let me know if my syntax is wrong? It seems to flow well when i read it. Here is my code:
import java.util.*;
import java.io.*;
public class BalancedParanthesis {
static String expressions[];
static class MyArrayStack{
int top = -1;
char brackets[] = new char[100];
void push(char x) {
if(top ==99) {
System.out.print("Stack full");
}
else {
brackets[++top] = x;
}
}
char pop() {
if(top ==-1) {
System.out.println("ERROR:Stack undeflow error");
return '\0';
}
else {
char element = brackets[top];
top--;
return element;
}
}
boolean isEmpty() {
return(top==-1)?true:false;
}
}
static boolean isMatchingPair(char bracketOne,char bracketTwo) {
if(bracketOne=='('&& bracketTwo ==')') {
return true;
}
else if(bracketOne == '{'&& bracketTwo == '}') {
return true;
}
else if(bracketOne == '['&& bracketTwo ==']') {
return true;
}
else {
return false;
}
}
static boolean isBalanced(String expression) {
MyArrayStack st =new MyArrayStack();
for(int index = 0; index<expression.length();index++) {
if(expression.charAt(index)=='{'|| expression.charAt(index)=='(' || expression.charAt(index)=='{') {
st.push(expression.charAt(index));
}
if(expression.charAt(index)=='}'|| expression.charAt(index)==')'||expression.charAt(index)=='}'){
if(st.isEmpty()) {
return false;
}
}
else if(!isMatchingPair(st.pop(),expression.charAt(index))) {
return false;
}
}
if(st.isEmpty()) {
return true;
}
else {
return false;
}
}
public static void main(String[] args) {
System.out.println("Input: ");
Scanner input = new Scanner(System.in);
String x = input.next();
if(isBalanced(x)) {
System.out.println("\n Expression: " + x + " is balanced");
}
else {
System.out.println("\n Expression: " + x + " is not balanced");
}
}
}
Andreas :
See logic flaws in comments:
// why are we checking for `{` twice here?
if(expression.charAt(index)=='{'|| expression.charAt(index)=='(' || expression.charAt(index)=='{') {
st.push(expression.charAt(index));
} // missing 'else' here, maybe??
// why are we checking for `}` twice here?
if(expression.charAt(index)=='}'|| expression.charAt(index)==')'||expression.charAt(index)=='}'){
if(st.isEmpty()) {
return false;
}
// shouldn't we be calling pop() and isMatchingPair() here?
}
// here we know that charAt(index) is not a ')' or '}',
// so why are we calling pop() and isMatchingPair()?
else if(!isMatchingPair(st.pop(),expression.charAt(index))) {
return false;
}
// isEmpty() returns a boolean, so why not return it directly?
if(st.isEmpty()) {
return true;
}
else {
return false;
}
For better performance and code clarity, only call expression.charAt(index)
once.
char ch = expression.charAt(index);
if (ch == '(' || ch == '{' || ch == '[') {
st.push(ch);
} else if (ch == ')' || ch == '}' || ch == ']'){
if (st.isEmpty()) {
return false;
}
if (! isMatchingPair(st.pop(), ch)) {
return false;
}
}
return st.isEmpty();
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=400302&siteId=1