1

I made simple calculator for android for sake of learning but its buggy. All number keys just append respective number to TextView. Equals button does the job of adding/subtracting/divide/multiply. But the problem is, it can do only one at a time, if I mix plus operation with minus, it will crash. Here is code of equals button performing addition:

equals = (Button) findViewById(R.id.equals);
equals.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        result.getText();
       String expression = (result.getText()).toString();

        if(expression.contains("+")){
        String[] numbers = expression.split("\\+");
        String no1 = numbers[0];
        String no2 = numbers[1];

        Double numb1 = Double.valueOf(no1);
        Double numb2 = Double.valueOf(no2);
        Double added = numb1+numb2;

        theresult = String.valueOf(added);
        result.setText(theresult);


        }
    }
});

as you can see above, another problem arises. It cant handle more than one + sign, how can I make it work to add x amount of numbers? Sorry I am new to programming, instead of just making it work, I want to learn how it will work so please explain too if possible. Thanks.

Shahbaz Talpur
  • 315
  • 4
  • 17
  • If its crashes then best idea is to post logcat error too. – Rohit5k2 Aug 10 '15 at 12:48
  • try **String entered = result.getText().toString()**. But if you add minus or any other symbols other than **+** it will not work – Emil Aug 10 '15 at 12:55
  • @Rohit5k2 Ok. Do you think that you will get the entered text from an EditText by calling this **String.valueOf(result)**?? – Emil Aug 10 '15 at 13:04
  • Sorry I copied this copied from my previous question. I fixed it now, same as what Boss said, added .toString() . Its TextView not a EditText, because EditText pops up keyboard thats pretty annoying and can throw exception if someone enters wrong stuff. – Shahbaz Talpur Aug 10 '15 at 13:56

2 Answers2

0

If its crashes then best idea is to post logcat error too.

Now the problem; you are separating the expression based on + only. so If you add - then the second part of the array would contain - also which can't be converted into double. Here you are getting the error.

So best idea is to separate the digit only part based on all the expressions such as +, -, * and /. Once all the expression has been separated then only you can convert the remaining values to string and perform the action based on respective expression.

You will have to write some code for all this.

Now resources that could help you. (Second is shorter approach)

  1. Evaluating a math expression given in string form

  2. How to parse a mathematical expression given as a string and return a number?

Community
  • 1
  • 1
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
  • Yes that's where I get error. It thorws NumberFormatException following by message invalid double "5-1" etc. How can I separate digit part based on expressions? Can you tell me please, not sure how to do it. And even if I do it, how can I know which number followed which operator? `+`,`-`,`*` or `/`? – Shahbaz Talpur Aug 10 '15 at 13:16
  • @ShahbazTalpur: Please go through the link I have posted. Its pretty good. See the accepted answers. – Rohit5k2 Aug 10 '15 at 13:17
  • In the first link, in accepted answer `ScriptEngineManager` is used, thats not available for Android. Second link is interesting but how I can install it? I moved downloaded .jar file to jre/lib/ext folder but typing bsh.Intrepreter gives cannot resolve error. – Shahbaz Talpur Aug 10 '15 at 13:49
0

This is best suited as a comment, I decided to put it as an answer just to not get it lost since there are already some answers.

Have a look at this question; the accepted answer might not be what you want because you want to learn, but there is an answer by boan that is possibly what you are looking for. I am not copying it here because it is already on S.O.

Community
  • 1
  • 1
innoSPG
  • 4,588
  • 1
  • 29
  • 42
  • Do you have any idea that you are posting link of this question only? please verify before you post. – Rohit5k2 Aug 10 '15 at 13:04
  • @Rohit5k2, I agree with you. But as you can read, I explained the reason in the post. I did not flag the question as duplicate because it is not really a duplicate. I do not even expect him to accept this answer. – innoSPG Aug 10 '15 at 13:09
  • @innoSPG: that's fine but I just said you should verify the link before posting. – Rohit5k2 Aug 10 '15 at 13:10
  • I am new to programming, answer by boan is for experienced one. Some parts I don't understand: 1. `int pos = -1, c;` What this comma and c means? 2. `c = (++pos < str.length()) ? str.charAt(pos) : -1;` Dont understand this 3. Few more :( – Shahbaz Talpur Aug 10 '15 at 14:01
  • @ShahbazTalpur, `int pos = -1, c;` is simply defining 2 variables on a single line; the variables `pos` and the variable `c`; It is not a very common practice in javaworld to define 2 variables in the same statement, but you can do it if you want.. – innoSPG Aug 10 '15 at 14:07
  • `c = (++pos < str.length()) ? str.charAt(pos) : -1;` is the ternary operator also known as the shorten `if-then-else` it is another form of `if(++pos < str.length()){c=str.charAt(pos);}else{c=-1}` – innoSPG Aug 10 '15 at 14:13
  • Oh thanks, it means `pos = -1` and `c=0`, now understood it What is `++pos` is doing? I have only heard of `pos++` that adds one to it. – Shahbaz Talpur Aug 10 '15 at 14:26
  • @ShahbazTalpur, you still have a lot to learn. `++pos` increments before the operation while `pos++` increments after the operation. It means that `if(++pos < str.length())` is testing using `pos+1`. If it was `if(pos++ < str.length())` then it will be testing using the present value of `pos`; In both case, the variable `pos` is incremented. What makes the difference is which value the statement is using. – innoSPG Aug 10 '15 at 14:29
  • @ShahbazTalpur, do not forget to accept the answer if it helped you solving your problem. That will help other people who have the same question as yours. If you have other question, feel free to ask, I will answer when I can. – innoSPG Aug 10 '15 at 14:59
  • @ShahbazTalpur, did this help solving your problem? If it helped, please accept it to help those who have similar problem. If it did not help, please let me know so that I can delete it to save people from wasting their time looking at it. – innoSPG Jan 26 '16 at 17:40