Results 1 to 7 of 7
  1. #1
    tim91700 is offline Member
    Join Date
    Nov 2013
    Posts
    2
    Rep Power
    0

    Default Split string from math operation

    Hello,

    I'm making a calculator with socket.

    I have the server and the client.
    Before to send an operation, I need to know if the operation is correct or not.

    I count '(' and ')', but I need a regex or a String[], to split the string.

    My string is : ((1+5)*(7*5))-3. The aim is to have : (,(1+5),*,(7*5),),-,3

    Or, I search for the last '(', find the next ')', solve it, and replace with the before and after part.
    And this again and again, until there is one or more signs and more than 2 numbers.

    The numbers can be float, so if there is a ',' , I need to add it in the previous string.

    Can you help me, because I try the seond solution without success ?

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,512
    Rep Power
    5

    Default Re: Split string from math operation

    Evaluation of expressions is typically done by converting the expression to Reverse Polish Notation (RPN - aka as postfix notation). So your expression
    ((1+5)*(7*5))-3 is changed to 1 5 + 7 5 * * 3 -. Then the evaluation is trivial using a stack. There are lots of articles on it so you can search the web for RPN.

    Also, I would suggest you split the tokens using the operators and parentheses as delimiters (but preserving the delimiters). This can be accomplished using a regex. It also precludes you from having to explicitly parse floating point numbers which can be a bit nasty.

    Regards,
    Jim
    The Javaô Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,439
    Blog Entries
    7
    Rep Power
    20

    Default Re: Split string from math operation

    Maybe my old blog article can be of any help: it describes a rather complete expression parser and several ways to evaluate the expressions. See the link on the top right side of this reply.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,512
    Rep Power
    5

    Default Re: Split string from math operation

    I haven't read your article but I knew you had written one from previous posts. Unfortunately, I didn't know how to reference it.

    Regards,
    Jim
    The Javaô Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,439
    Blog Entries
    7
    Rep Power
    20

    Default Re: Split string from math operation

    Quote Originally Posted by jim829 View Post
    I haven't read your article but I knew you had written one from previous posts. Unfortunately, I didn't know how to reference it.
    Neither do I ;-) I simply mention the 'blog entries' link near the top right corner of my replies.

    kind regards,

    Jos (<--- lazy bum)
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    tim91700 is offline Member
    Join Date
    Nov 2013
    Posts
    2
    Rep Power
    0

    Default Re: Split string from math operation

    Hello,

    I have the code to solve RPN : Reverse Polish Notation : Algorithms¬*¬ę¬*Collections Data Structure¬*¬ę¬*Java

    I use this code to make the postfix change : http://stackoverflow.com/questions/1...fix-to-postfix

    Can you help me to cath all exceptions from the second link, because if the input is '((()', I have java.util.EmptyStackException.
    I mean to not return the string without the good notation.

    Regards
    Last edited by tim91700; 11-17-2013 at 09:13 PM.

  7. #7
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,512
    Rep Power
    5

    Default Re: Split string from math operation

    The JDK Stack class has an empty() method to tell if the stack is empty. So you can couple that with parentheses counting to ensure that you have the correct number. However, what if someone types in (3-5+) or (*3-5)? If you really want to ensure the expression is syntactically correct then you may need to write a state machine to help parse the expression. If carefully done it can be integrated into your RPN conversion.

    Regards,
    Jim
    The Javaô Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 2
    Last Post: 01-31-2013, 08:58 PM
  2. OUt of memery error while using string split operation
    By sheetalmuskan in forum New To Java
    Replies: 10
    Last Post: 07-12-2011, 12:36 PM
  3. Split a String with split()--Help
    By danilson in forum New To Java
    Replies: 7
    Last Post: 11-19-2010, 04:08 PM
  4. How to split a String using split function
    By Java Tip in forum java.lang
    Replies: 4
    Last Post: 04-17-2009, 08:27 PM
  5. How to split a String using split function
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 09:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •