0

I am very new to this sort of thing and am trying to teach myself but have gotten stuck here. I am writing an app which will calculate the size battery required. The calculator side of things i have done and it is working however i want to add a feature that when the answer is displayed, it will tell you what part number you need for the battery required. I have tried to work this out myself and failed miserably so i am hoping someone may be able to point me in the right direction

The answer from the battery calculator shows in ahresult. what i want is battreq to show the part number required for the correct size battery. so for example, if the calculator showed that a 2.43Ah battery was required, battreq would show "A 3.2ah battery is required" as that is the next size available.

ahresult = (TextView) findViewById(R.id.ahresult);

        battreq.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Integer.parseInt(ahresult.getText().toString());
                if (ahresult.equals(>= "0.00" && <=  "1.20")){
                    battreq.setText("A 1.2Ah battery is required");
                }
                else if (ahresult.equals (>= "1.21 && <= 2.10")){
                    battreq.setText("A 2.1Ah battery is required");
                }
                else if (ahresult.equals (>= "2.11 && <= 3.20")){
                    battreq.setText("A 3.2Ah battery is required");
                }
              // so on and so on with different battery sizes
            }

Here is my (poor) effort at having a go but i am getting illegal start of expression.

Could someone guide me in the right direction please?

EDIT: Still seem to be having issues with this despite the help here. Below is the updated code. Have i got anything wrong here?

ahresult.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                double result = Double.parseDouble(ahresult.getText().toString());

                if (ahresult.equals(result >= 0.001 && result <=1.200 )) {
                    battreq.setText("1.2Ah req");

                }
                else if (ahresult.equals(result >= 1.201 && result <=2.100 )) {
                    battreq.setText("2.1Ah req");
                }
            }
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
Harni
  • 11
  • 4

2 Answers2

1

You need change this

battreq.addTextChangedListener(new ...

With this

ahresult.addTextChangedListener(new ...

You need listen the ahresult's value and change the battery needed.

In the "onTextChanged" the "CharSequence s" param is the ahresult's text

JaFer
  • 213
  • 2
  • 10
  • Thanks JaFer, That makes complete sense now you say it! Im still not expressing this correctly if (br1.equals(>= "0.00" && <= "1.20")){ battreq.setText("A 1.2Ah battery is required"); How would i go about showing that i want it to show this text if the anwer is within the range of 0.00 and 1.20? – Harni Feb 16 '18 at 20:53
  • You can't do this you need check one by one. If your value to check is the variable named "br1", first you need to convert it from String type to Double. >double br1Double = Double.valueOf(br1) Next check the br1Double value > If (br1Value >= 0.00d && br1Value <= 1.20d) – JaFer Feb 16 '18 at 22:13
1

Firstly, you should add addTextChangedListener to ahresult instead of battreq.

Secondly, you have to parse the changed text s to Double then compare within your desired range:

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

    Double result = Double.parseDouble(s.toString());

   if (ahresult.equals(result >= 0.00 &&  result <=  1.20)){
       battreq.setText("A 1.2Ah battery is required");
   }
   else if (ahresult.equals (result>= 1.21 && result <= 2.10)){
       battreq.setText("A 2.1Ah battery is required");
   }
   else if (ahresult.equals (result>= 2.11 && result <= 3.20)){
       battreq.setText("A 3.2Ah battery is required");
   }
   // so on and so on with different battery sizes
}
tahsinRupam
  • 6,325
  • 1
  • 18
  • 34
  • Thanks, thats really useful. I still can not get it to work though! I am getting no errors which is an improvement but battreq is not doing anything! – Harni Feb 16 '18 at 22:10
  • Can you log print the value of the `ahresult` ? And also the parsed value? This might help. – tahsinRupam Feb 17 '18 at 16:13
  • Sorry to sound stupid but how would i do that? This is my first app so a steep learning curve!! – Harni Feb 17 '18 at 16:43
  • ex: `Log.d("Testing->", s.toString());` and check the result in logcat – tahsinRupam Feb 17 '18 at 20:26
  • Sorry, no matter where i insert this, it just will not work! I have also added my most up to date code to the original post – Harni Feb 18 '18 at 14:04
  • May be because `ahresult`'s text is not changing! Btw, why are you putting textChangeListener to a textView? Shouldn't it be an `editText`? – tahsinRupam Feb 18 '18 at 18:21
  • 'ahresult' is a 'textview' that is populated from a calculation from 3x editText's. What i am after is when someone hits button 'calculate', it populates 'ahresult' with the answer but also populates 'battreq' with the battery size required if that makes sense? – Harni Feb 18 '18 at 18:35
  • Ignore my last comment, ive worked out where i was going wrong and have fixed it. Thanks for your help! – Harni Feb 19 '18 at 15:30
  • Thank you too. You've been very clear of your requirements! – tahsinRupam Feb 20 '18 at 07:15