• Post Reply Bookmark Topic Watch Topic
  • New Topic

Split a math expression  RSS feed

 
Charlie Wall
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys

I have a small (well big to me) problem that I need some help with
I have a math expression in a String, that I want to split into 4 pieces as in the following example:

String math = "23+4=27"
int num1 = (23 STORES HERE)
int num2 = (4 STORES HERE)
String operator = (+ STORES HERE)
int answer = (27 STORES HERE)

Note that the operator can be either + - * /

I appreciate your help
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have some ideas, but post what you've tried first.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class java.lang.String has many useful methods to break a string into parts. Have a look at the documentation of java.lang.String to see which methods might be useful for your purpose. Try them out, and let us know how it's going.
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you know regexes from, say, Linux/GNU, you could use java.util.regex.Pattern.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
…and if you don't know about regexes, try this tutorial.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you simply splitting the String into 5 tokens? If you have expressions more complicated than
23+4=27
then you will need to learn about parsing, which is much more complicated.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!