Omar Wael Omar Wael - 4 months ago 25
Java Question

Calculate a formula from a string

I want to do calculator that do sums but just in one index like this:

args[0] = "2+2"


and the output should be
4
.

Answer

I won't do your homework for you, but I'll split it in few steps:

  1. You should get String before the "+" and after the "+":

Have a look at String.indexOf and String.substring for that. Make sure you check for -1 for indexOf.

  1. Parse String to int:

int foo = Integer.parseInt(String s);

  1. Now you can add two ints.

Good luck!