1 Replies - 518 Views - Last Post: 17 November 2017 - 05:51 PM

#1 edendiverse   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 17-November 17

Calculator project - can't have two operators in a row

Posted 17 November 2017 - 02:29 PM

I have a plus button on my calculator. The first statement is to add a "+" sign when there is no input (<calculation string variable holds all input from user). The second statement is to prevent the user from typing 2 "+" signs in a row by adding "".
It keeps giving me an error: java.lang.StringIndexOutOfBoundsException: length=0; index=0
I don't quite understand this.

        btnPlus.setonclickListener(new View.onclickListener() {
            @Override
            public void onclick(View view) {
                calculation = tvCalculation.getText().toString();

                Character lastChar = calculation.charAt(calculation.length());

                //App negeert deze statements
                if(calculation.length() >= 0)
                {
                    tvCalculation.setText(calculation + "+");
                }
                else if (lastChar.toString().equals("+"))
                {
                    tvCalculation.setText(calculation + "");
                }
            }
        });


Is This A Good Question/Topic? 0
  • +

Replies To: Calculator project - can't have two operators in a row

#2 Radius Nightly   User is offline

  • D.I.C Regular

Reputation: 33
  • View blog
  • Posts: 291
  • Joined: 07-May 15

Re: Calculator project - can't have two operators in a row

Posted 17 November 2017 - 05:51 PM

First, your if - else if are made to do that. Error probably happening at the calculation variable thats not defined at the beginning or so, it means its not empty (zero characters, length 0), but information does not exist, like nil, or you cant convert to string nothingness.
Adding calculation variable + "+" is not a good way and you should remove else if, there is no point in adding "".

For "+" operator i would go with if calculation length > 0 and last char ~= "+" {add text "+"}, end.
So writing +2+2 are not possible, for a "-" operator it will be good to have ">=" instead of ">", to be able to write eg. -2+2...
And also this has to be valid, if last character is not "+", then add "+", if both of this are wrong (mainly for second one), "+" wont be added.

Using a set text by coping previous one to add something is not the way, you should avoid using settext (except on paste) and use something to add text and clear text, at the end of line (except if user can select in middle of calculation to add something), but point is simple, try to use add text thing, im not programming in Java, but im sure it have that function.

Second, instead of text box, why dont you try to work with input and avoid coping same variable over and over, and try to use less conversation into strings or whatever. You cant have this copy things on each number and each operator, if a phone gets a little lag or if its slow, typing in the middle of copy-paste thing with multiple operators leads to break whole calculation line, you can make input available only for a specific things (like 0-9, operators, dots, nothing else), and then you can easy make operators working, just making short if statement, if should it add operator or not, also all operators can be add in a single if, instead of each operator for itself, like, single if for all operators and should it add chosen operator or not.
Copy should work fine, but its not how it should be used, its good for local variables what will not be written down (as a text), and will be just in memory, like i = i + 1, games for gasoline for like flame thrower "bullets", or test guns that "bullets" are electricity, using precise of +0.00000001 calculation, but when you have to write it down, even graphic card will slow you down making lags, imagine your job, you have a book, and you want to add dot at the end of a book story, at the last page, what you are doing now is rewrite a whole book as a new, just to add a single dot at the end, instead of just adding dot at the end of book. What would you choose, usually like in school, if something is missing, you add it, simple as that.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1