• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...

 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
    Posts: 6
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Marshal
    Posts: 79179
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 6
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Marshal
    Posts: 79179
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 6
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
    reply
      Bookmark Topic Watch Topic
    • New Topic