Click to See Complete Forum and Search --> : doesnt remove this


Techno
April 6th, 2004, 10:09 PM
hi there

for some reason, my code will not remove any ( from the stack. it only does one then thats it! :(

and if i do another stack.peek() statement i get a runtime error! if i comment it out, it works fine, until i pass the expression to the other file but obviously wont compute since it has a ( in the expression

ihave attached the code, since its a bit long to paste it here

basically im trying to get it to work with the ()

dlorde
April 7th, 2004, 05:50 AM
I think the problem may be that the StringTokenizer isn't doing what you think it is... Try printing out the tokens as they are extracted. Maybe you should consider usind String.split(...) with a regular expression, or parse the string yourself.

Incidentally, it's recommended that you don't directly compare expressions and values with boolean 'true' and 'false'. If an expression is a boolean, you don't need to compare it - e.g.if (expr()) {
...
}

not

if (expr() == true) {
...
}

and

if (!expr()) {
...
}

not

if (expr() == false) {
...
}

and

return expr();

definitely not

if (expr()) {
return true;
}
else {
return false;
}
However, a cleaner, more flexible way of checking for an operator is to put the list of operators into an array:String[] operators = { "+", "-", "*", "/" ...};

boolean isOperator(String op) {
for (int i=0; i < operators.length; i++) {
if (op.equals(operators[ i ])) {
return true;
}
}
return false;
}The highest reward for a person's toil is not what they get for it, but what they become by it...
J. Ruskin

Techno
April 7th, 2004, 07:14 AM
thanks. we werent taught like that so i cant say much about that :)

i have printed out the contents of the tokenizer when its splitting it but still makes no difference. really dont know why its not removing the ( when it should. it does for the first time but when it comes round again in the while loop it doesnt.


if i do:


( a + b )


it does it fine


if i do:


( a + b ) - ( v * d )

then thats where the problem happens

dlorde
April 7th, 2004, 08:30 AM
When I run through the example you posted, the 'numbers' string ends up as "a ( b + v d * - ". The logic looks very confused - I suggest you break the evaluation method up into a number of separate methods. The opening parenthesis gets copied to the 'numbers' string because it gets pushed on the stack when first read, then when the next operator token (+) is found, it is popped off the stack added to the 'numbers' and the new operator is pushed onto the stack. The same thing happens with the example you say is OK: numbers = "a ( b +". Looks like a logic error to me, but without an example of the correct output, it's hard to say.

Why not just step through it for yourself with a debugger (or failing that, a pencil and paper), and see what's going wrong?

I feel like the blind man in a dark room looking for a black cat... that isn't there...

Techno
April 7th, 2004, 08:31 AM
sure - thanks :)

Techno
April 7th, 2004, 08:55 AM
this is sooooooooooooooooooooooo wired

i deleted the code for the brackets completly so it was in the state it was originally


compiled


ran it

entered expresion with brackets

i expected a compiler error

but no! it works fine!!!!! wierd!!!!! o_0

Techno
April 7th, 2004, 09:26 AM
or maybe not lol

hm i still cant figure out why its not popping it off, i deleted the code i had and re started the bracket code.


..
if(isOperator(token))
{

//check for brackets

//if the bracket is an opening bracket then push it on the Stack.

if(token.equals("("))
{
//push it on the stack
theValues.push(token);

}




//check to see if a closing bracket was found

if(token.equals(")"))
{
//flush everything from the stack
ConsoleIO.out.println("Closing bracket detected");
flushStackContents = bracketCloseFlushStack();

}

..
..
..

public String bracketCloseFlushStack()
{
//declare variable
String itemString = "";

ConsoleIO.out.println("in the bracketCloseFlushStack() method");

if(theValues.peek().equals("(")) //if the stack peek has an opening bracket
{

//pop it off because we dont need the (. Invalid RPN expression
theValues.pop();

ConsoleIO.out.println("popped the top value off stack");

//flush everything clean from the stack because
//that is the way it works and mentioned in notes.

while(!theValues.isEmpty()) //while the stack is not empty
{
String itemPopOff = (String) theValues.pop(); //pop off the next item
//display output for debugging purposes

ConsoleIO.out.println("\nitem popped off in the flush method: " + itemPopOff);

itemString = itemString + itemPopOff + " "; //add it to the string
}

}

//display the String
ConsoleIO.out.println("\n\ntotal itemString output: " + itemString);

return itemString;

}


but it wont see or pop of the ( from the stack

cjard
April 7th, 2004, 02:44 PM
why does your indentation run all over the place? why does your first if statement if(token.equals("(")) not have the same indentation level as the rest? why does the method not return or escape at this point? if the token is a ( then why bother testing it to be something else?

TheCPUWizard
April 7th, 2004, 02:54 PM
dlorde <- that is the correct order (if he is parsing to RPN which is likely)


cjard <-0 to get the indentation to look good you have to match your tab settings to his [which is why one should ALWAYS use spaces and NEVER tabs, check your IDE settings Techno
:) ]

Techno
April 7th, 2004, 03:43 PM
hmk

anyway

i still cant get it to work, i thought i had it but nope :(

i dont know how to do an evaluation with brackets. ive re written an re thought the logic again and again and i just cant do it :(

dlorde
April 8th, 2004, 11:02 AM
Originally posted by TheCPUWizard
dlorde <- that is the correct order (if he is parsing to RPN which is likely) Oh, OK. I thought one of the points of RPN was that it didn't need parentheses...

Simplicity does not precede complexity, but follows it...
A. Perlis

TheCPUWizard
April 8th, 2004, 11:43 AM
That is correct....


(A+B) * (C+D) = "standard algeberic notation...needs parens

A B + C D + * = RPN No Parens needed


I believe this is the transform the OP is working on....

dlorde
April 8th, 2004, 06:13 PM
Originally posted by TheCPUWizard ...
I believe this is the transform the OP is working on.... I see. A work still in progress then... ;)

If you're not failing every now and again, it's a sign you're not doing anything very innovative...
W. Allen

Techno
April 8th, 2004, 06:50 PM
ok, lets forget the initial post for now :)

i have no idea how to start to implment the feature with the ()

i have tried 10 different ways, the last one i was SOO close but never worked

i am so tired, been at it for 3 days and yes ive had breaks but time is nearly up for this. i really have no idea how to implement the feature in the calc to include and deal with ()

cjard
April 9th, 2004, 07:29 AM
Originally posted by TheCPUWizard
dlorde <- that is the correct order (if he is parsing to RPN which is likely)


cjard <-0 to get the indentation to look good you have to match your tab settings to his [which is why one should ALWAYS use spaces and NEVER tabs, check your IDE settings Techno
:) ]

indentation is mismatched no matter what? im looking at the IE indentation.. but i do agree with you; Techno, tab size varies per machine, space are always a fixed size. cahnge your editor to use spaces instead of tabs, and use 2, 3 or 4 spaces ofr indenting