Results 1 to 8 of 8
  1. #1
    anthropamorphic's Avatar
    anthropamorphic is offline Senior Member
    Join Date
    Jun 2011
    Posts
    135
    Rep Power
    0

    Default Convert A String To Math Problem

    I figured it out thanks!
    Last edited by anthropamorphic; 07-06-2011 at 05:42 AM.

  2. #2
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Are the spaces there in every string? If there was you could string.split your string into an array. Then you would just have to figure out which ones were numbers and which ones were symbols.

    Take a look at the api: String (Java Platform SE 6)
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  4. #4
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Hmm, that is definitely a more simple approach. I guess I learned something today
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  5. #5
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    No bug ever had to calculate its fitnessfunction.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    If you cannot use Javascript, what you need to do is convert your infix (2 + 3) expression into a postfix (2 3 +) expression. Then you can evaluate the expression using a Stack. For each operand (number) push onto Stack. For each operator (+-*/etc) pop off 2 values from the Stack, perform operation, push result back onto Stack. Once you are done the only value left on the Stack should be the result. If you Google you should find plenty of info on how to do this.

  7. #7
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    what you need to do is convert your infix (2 + 3) expression into a postfix (2 3 +) expression.
    If you need to make a parser this is A possibility (and probably the easiest to program). But you can also make other (prefix I guess) parsers. I did it myself, is was quite a project but it works great, and is far easier for the users (including me).
    It is one of the projects I still borrow from the most in other projects (currently a matrixcalculator: I can just use sin2pi or sin(2pi) in my matrix which is much more palatable then 2 pi * sin (or something like that)).
    Last edited by Jodokus; 07-05-2011 at 01:57 AM.
    No bug ever had to calculate its fitnessfunction.

  8. #8
    anthropamorphic's Avatar
    anthropamorphic is offline Senior Member
    Join Date
    Jun 2011
    Posts
    135
    Rep Power
    0

    Default

    Thanks for all the suggestions I'll try them out and hopefully I will be able to figure it all out. (I'm not the best at java yet)

Similar Threads

  1. Hot to convert string to int ????
    By java_no1 in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 06-07-2011, 07:40 AM
  2. convert obj to string
    By isme in forum New To Java
    Replies: 11
    Last Post: 06-14-2010, 10:54 AM
  3. Replies: 2
    Last Post: 09-29-2009, 02:11 AM
  4. Math Related Problem
    By perito in forum Advanced Java
    Replies: 1
    Last Post: 03-21-2008, 08:53 AM
  5. Math Related Problem
    By perito in forum New To Java
    Replies: 3
    Last Post: 03-20-2008, 05:22 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
  •