0

I m getting a small error on my code. When I type in 8*(9-2). I am getting the wrong answer. I think it because of this if statement-

if ((token.getName().equals("rparen") || 
     temp.equals("lparen")) || 
    (token.getName().equals("rbracket")||
     temp.equals("lparen")) || 
     token.getName().equals ("rcurley"))

When I delete the temp.equals("lparen") it works correctly. But when I type in another equation ([57 -50)*4+8]*4, I am getting a 144 answer but it is supposed to be an error message.. Below is my method:

public double factor () 
{ 
    double result = 0; 
    if (token.getName ().equals ("digit")) 
    { 
        result = token.getValue (); 
        System.out.print ("" + token.getValue () + " "); 
        token.getToken (); 
    } 
    else 
    if (token.getName ().equals ("lparen")||token.getName ().equals ("lbracket")||token.getName ().equals ("lcurley")) 
    { 
        String temp =token.getToken();
        token.getToken (); 

        if ((token.getName ().equals ("rparen")||temp.equals("lparen"))||(token.getName ().equals ("rbracket")||temp.equals("lparen"))||token.getName ().equals ("rcurley")) 
            token.getToken (); 
        else 
            System.out.print ("Error - missing right paren"); 
    } 
    else 
        System.out.print ("Error - invalid token"); 
    return result; 
} // method factor 
Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
  • So what is a `token`? `StringTokenizer`? `String[]`? `String`? `Token`? – Makoto Dec 01 '13 at 02:01
  • It seems like `token.getToken ()` does change state (because else your single `token.getToken ();` lines would not have any effect). [That is very bad design](http://stackoverflow.com/questions/13876066/is-it-bad-practice-to-have-my-getter-method-change-the-stored-value). Maybe you want to rename it to `next()` or `consumeCurrentToken()` or something like that. – Domi Dec 01 '13 at 02:04
  • Left parenthesis doesn't occur in 'factor'. It occurs in 'primary'. And you aren't handling * or / in 'factor', which is what it's for. I suggest you post the rest of the code. – user207421 Dec 01 '13 at 02:47

1 Answers1

0

I am guessing here but it seems like you are missing a recursive call. The value between two parentheses is supposed to be any valid expression. It seems like you want to change

String temp =token.getToken();

to:

moveToNextToken();     // or however you call this method
result = factor();     // to read any valid factor inside the parentheses

or possibly:

moveToNextToken();         // or however you call this method
result = anyExpression();  // to read any valid expression inside the parentheses

Also, the if that checks a matching closing parenthesis seems wrong. It should be a mixture of or's and and's, not just or's:

(openingToken.equals ("rparen")|| currentToken.getName().equals("lparen"))

change to:

(openingToken.getName().equals ("rparen") && currentToken.getName().equals("lparen"))

If you don't, you are not really matching opening and closing parentheses.

Domi
  • 22,151
  • 15
  • 92
  • 122
  • I did that above but I am still received the wrong answer. The answer I am getting is -2.0 – user2803053 Dec 01 '13 at 02:23
  • Yes I did and it says something like ("Error - missing right paren") – user2803053 Dec 01 '13 at 02:48
  • I am guessing it is because you consumed the right parenthesis too early and it is gone by the time you return from `anyExpression`. Does that ballpark it? Also, check my latest edit on the closing token. – Domi Dec 01 '13 at 02:50
  • I try that method also with this below else – user2803053 Dec 01 '13 at 03:08
  • if (token.getName ().equals ("rparen")&&temp.equals("lparen")||(token.getName ().equals ("rbracket")&&temp.equals("lparen"))||token.getName ().equals ("rcurley")) – user2803053 Dec 01 '13 at 03:10
  • That one is missing proper parentheses. You want: `(a && b) || (x && y)` – Domi Dec 01 '13 at 03:23
  • Ok could you show me the same way you did with the method that you use and apply it with the code I have. Because i, trying to get it but it is not working out correctly. – user2803053 Dec 01 '13 at 03:46