Results 1 to 5 of 5
  1. #1
    lemon3 is offline Member
    Join Date
    Jul 2008
    Posts
    3
    Rep Power
    0

    Red face create a formula from a string

    Hi im doing this statistical program where i want the user
    to be able to make hes own formula and pass it to the program.

    i am able to get it from a jlabel to a string, but i cant use the string
    as a mathematical formula..
    im supposed to use tokens (so i heard),
    but i dont know how ??
    can anyone help me, i want to have it as simple as possibly by the way,
    because i am new at java so this is a kind of learning program for me.

    thanks guys and gurls

  2. #2
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default Formulas

    Hello lemon3. :)

    I wrote I program in C# that can parse (meaning to decode from a string) basic mathematical formulas and then calculate the answer step-by-step while keeping the order of operations correct, e.g. multiply and then add, etc. It could also handle brackets. So the user would input
    Java Code:
    1 + 5 * (1 - 3 / 3)
    and the program would give
    Java Code:
    1 + 5 * (1 - 3 / 3)
    = 1 + 5 * (1 - 1)
    = 1 + 5 * (0)
    = 1 + 5 * 0
    = 1 + 0
    = 1
    If this is what you want to do, then it will take some time to create such a system. The parsing and execution is the hardest bit but this is still a large undertaking when starting from scratch.

    Unfortunately, my code is in C#, so it would not help much.:o
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,255
    Rep Power
    25

    Default

    That sounds like a difficult way to do it. Reading in an expression as a string and then parsing it is not trivial!! I'd suggest that you simplify and restrict the user input in some way so that it doesnt' require parsing. For example use a form with mulitple text input areas and operand selections. Or find someone else's code that will do the parsing and evaluation and use that.
    There was a student looking for a project on one of the forums. Find him and see if he will do it.

    Look at:
    SourceForge.net: Files
    Last edited by Norm; 07-17-2008 at 07:03 PM.

  4. #4
    lemon3 is offline Member
    Join Date
    Jul 2008
    Posts
    3
    Rep Power
    0

    Default

    thanks for your help, i found some evaluation program called jel
    i will try to use that one . thanks

  5. #5
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default

    I'm glad to be of assistance, lemon3. :D
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

Similar Threads

  1. Replies: 0
    Last Post: 04-12-2008, 08:39 PM
  2. Replies: 1
    Last Post: 04-09-2008, 02:15 AM
  3. Replies: 0
    Last Post: 11-20-2007, 04:59 PM
  4. Replies: 1
    Last Post: 08-03-2007, 10:52 AM
  5. What is the formula?
    By yuchuang in forum New To Java
    Replies: 3
    Last Post: 04-30-2007, 10:00 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
  •