0

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.

Omar Wael
  • 19
  • 1
  • 5
  • 1
    What you have tried, show it here – Vickyexpert Jul 19 '16 at 09:05
  • 1
    make your question more clear. and go through http://stackoverflow.com/help/how-to-ask – Abhishek Jul 19 '16 at 09:05
  • 4
    Take a look at [this post](http://stackoverflow.com/questions/3422673/evaluating-a-math-expression-given-in-string-form). – kazbeel Jul 19 '16 at 09:05
  • There is no "easy" way to do this; and **evaluating** strings is definitely something on a "I am a beginner in Java" level. So, two options only: A) you forget about this idea for now; and you focus on learning more of the basic java skills B) you bite the bullet and try to understand what the post Woozy is linking to actually means. Which will be probably really hard for you. Thus, my personal recommendation: stick with A for some more time; then go B. – GhostCat Jul 19 '16 at 09:07
  • @WoozyCoder thanks alot man you saved me – Omar Wael Jul 19 '16 at 09:07
  • @GhostCat i did everything to think about it even i have found methods but it was for strings not char but yes you are right – Omar Wael Jul 19 '16 at 09:10
  • 1
    @OmarWael Well, there are actually many options to approach your subject; and even the naive way how you asked your question implies that this is way beyond your current set of skills. But maybe I am wrong, and it will be easy and smooth for you based on that link ... but I somehow doubt that. You see, sooner or later, you will want (or will be asked to) extend that piece of code that you now intend to "copy" into your program. And that is kinda hard, when you don't understand how those things really work ... – GhostCat Jul 19 '16 at 09:13
  • @GhostCat i don't just copy paste i understand the code first then write it my self and thanks – Omar Wael Jul 19 '16 at 09:55

1 Answers1

1

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!

Daniel Stradowski
  • 3,466
  • 1
  • 12
  • 21