1

In Java If a GUI input is this (55.55 + 23424 * 4545) those inputs will be converted from String to integers. the thing is how can I make the program distinguish between the numbers and operators in this calculation?

  • 4
    Hopefully they won't all be converted to integers, because 55.55 isn't an integer... – DNA Mar 04 '12 at 17:05

3 Answers3

2

You will have to parse the String you receive before, getting the numbers and operators in distinct variables. It would probably be simpler to allow the user to enter only numbers and provide buttons for the operators so you can treat them as signals.

Just parsing this kind of String (if you allow all kind of operators and operations) would need eventually to write a complete grammar and generate a parser from that grammar.

talnicolas
  • 13,885
  • 7
  • 36
  • 56
2

Use regular expressions! Regular expression for numbers:

number: [0-9]+\.?|[0-9]*\.[0-9]+

See this tutorial for how to use regular expressions to properly parse strings.

smessing
  • 4,330
  • 1
  • 22
  • 19
2

I found an interesting idea here: Is there an eval() function in Java?

Using eval-analog in java.

Also there is a special eval project here:

http://java.net/projects/eval/pages/Home

Community
  • 1
  • 1
dhblah
  • 9,751
  • 12
  • 56
  • 92