aspose file tools*
The moose likes Beginning Java and the fly likes infix to postfix, need help catching errors such as having extra parenthesis Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "infix to postfix, need help catching errors such as having extra parenthesis" Watch "infix to postfix, need help catching errors such as having extra parenthesis" New topic
Author

infix to postfix, need help catching errors such as having extra parenthesis

Paul Nguyen
Greenhorn

Joined: Nov 02, 2010
Posts: 6
Alright, my brain is almost fried after hours of thinking and trying to get this to work, I was able to get my program to work with correct input, but I can't seem to get my program to catch extra parenthesis errors or errors in the infix notation. Help please



using this as a test program/driver, errors are described in comments:


any ideas on improving/simplifying the algorithm would be nice too, because mine looks like a mess :/

Edit: fixed one problem for input str7. Still working the rest...

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Why are you calling System.exit()? That can be a dangerous bit of code to use; if you are multi-threading and there is something critical in another thread, calling System.exit() may terminate that thread with its task incomplete.

What is your algorithm? Write it out in simple terms like this
  • Empty kettle
  • Put fresh water into kettle
  • Put kettle on
  • Put coffee powder in cup
  • Wait until kettle is nearly boiling
  • Take kettle off
  • Pour water into cup
  • Make sure kettle is not put back on, in case it boils dry
  • Add milk or sugar to coffee if required
  • Drink coffee
  • That algorithm will enable you to count brackets drink a cup of (instant) coffee. Please show us your bracket-counting algorithm similarly.
    Paul Nguyen
    Greenhorn

    Joined: Nov 02, 2010
    Posts: 6
    I think I've got it, but I created another method to check the parenthesis instead and then have it give me a boolean answer so I could implement it to my other method.

    Edit: I've been taught to put System.exit(0) at the end of my programs, so I'm just going with it for now. Is it that bad to use?




    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39436
        
      28
    That will tell you that your () match, but it won't help with this sort of thing.
    (1 + 2) * (3 + 4)
    You will correctly identify matched () but will incorrectly parse it as if you had one pair of brackets. You need to iterate the String until you reach the ) which matches your first (.

    I cannot remember whether you are pushing and popping in the right places; do you only have one stack? You will have to look in your books for the right way to push and pop ().

    In that location, System.exit(0) has no effect at all, but in a multi-threaded application, it might be positively harmful.
    Paul Nguyen
    Greenhorn

    Joined: Nov 02, 2010
    Posts: 6
    I just reuse one stack. After each method is done I clear it with stack.clear();

    my infix to postfix algorithm is now like this, and my infix string will add an extra set of matching parenthesis on the ends "(" + inFix + ")"

    if (parenthesis does not match)
    return (error in infix expression)
    else
    continue to convert infix to postfix method
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39436
        
      28
    I think you need to go back to square 1 with your pencil and paper, and write down an algorithm for matching brackets eg () [] etc. How are you going to be able to parse (1 + 2) * (3 + 4) and get the correct answer of 21?
    Paul Nguyen
    Greenhorn

    Joined: Nov 02, 2010
    Posts: 6
    Oh, we don't need to evaluate the expression for this project, just to convert the notation. My class already did postfix evaluation in a past project already. I've finished this project already, and thanks for the help Campbell.
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: infix to postfix, need help catching errors such as having extra parenthesis