-5

I'd like to know if you can somehow get something like

String test= "3445+100";
double x= test

so I can turn a typed in calculation into a result?

First time asking sorry if I waste your time :/

Lauritz
  • 1
  • 2
  • 3
    check this answer http://stackoverflow.com/questions/3422673/evaluating-a-math-expression-given-in-string-form – Igor Konoplyanko Oct 22 '14 at 11:11
  • Welcome to the community :). The basic answer would be "yes, you can", but you should show that you have tried to solve the Problem yourself. To get you started, though: look into the "split"-function for String to start off. – Layna Oct 22 '14 at 11:14
  • Also see http://stackoverflow.com/questions/15084984/ or http://stackoverflow.com/questions/1432245/ – vowel-house-might Oct 22 '14 at 11:14
  • Try using .split("\\+"); – Adz Oct 22 '14 at 11:14
  • You could use the Javascript enginge. Have a look here http://stackoverflow.com/questions/19261329/math-string-with-no-spaces#23652514 – SubOptimal Oct 22 '14 at 12:17
  • What is the syntax of permitted expressions? Are parentesese permitted? – Raedwald Dec 10 '14 at 13:17

2 Answers2

0
String test= "3445+100";
String[] numberArray=test.split("\\+");
int a = Integer.parseInt(numberArray[0]);
int b = Integer.parseInt(numberArray[1]);

int solution=a+b;
Bart Hofma
  • 644
  • 5
  • 19
0

You split the String between the operators. Then you cast the Number-Strings to int and you can cast the Operators with a distinction of cases because you can have only +,*,- and /.

Patricia
  • 2,885
  • 2
  • 26
  • 32