0

enter image description here

I have this code, which on my programme I am using check boxes. If check box is checked it must execute the statement in the if statement, but my program execute all the "if statements" even if the check box is not checked.

public void onClickMakeTransactionButton(){

        chkAirtime = (CheckBox) findViewById(R.id.chkAirtime);
        chkElectricity = (CheckBox) findViewById(R.id.chkElectricity);
        chkWater = (CheckBox) findViewById(R.id.chkWater);
        chkTransfer = (CheckBox) findViewById(R.id.chkTransfers);
        chkWithdrawal = (CheckBox) findViewById(R.id.chkWithdrawal);
        chkPayDstv = (CheckBox) findViewById(R.id.chkPayDstv);

        final TextView savingsBalance = (TextView) findViewById(R.id.txtSavingsBalance);
        savingsBalance.setText("Your Balance is: " + balance);

        btnMakeTransaction = (Button) findViewById(R.id.btnMakeTransaction);

        btnMakeTransaction.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                StringBuffer result = new StringBuffer();
//This are the if statements for my check boxes. My program executes the if statements even if the check boxes are not checked. what could be the problem. please help
                if (result == result.append("Airtime: ").append(chkAirtime.isChecked())) {
                    balance = balance - 50;

                }

                if (result == result.append("Electricity: ").append(chkElectricity.isChecked())) {
                    balance = balance - 150;
                }

                if (result == result.append("Water: ").append(chkWater.isChecked())) {
                    balance = balance - 150;
                }

                if (result == result.append("Transfers: ").append(chkTransfer.isChecked())) {

                    balance = balance - 500;
                }

                if (result == result.append("Withdrawal: ").append(chkWithdrawal.isChecked())) {

                    balance = balance - 200;
                }

                if (result == result.append("Pay Dstv: ").append(chkPayDstv.isChecked())) {

                    balance = balance - 200;

                } else {
                    Toast.makeText(SavingsAccountTransactions.this, "Nothing been Selected ", Toast.LENGTH_LONG).show();
                }


                savingsBalance.setText("Your Balance is: " + balance);
                Toast.makeText(SavingsAccountTransactions.this, result.toString(), Toast.LENGTH_SHORT).show();
            }
        });

    }
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Nhlanhla
  • 3
  • 1
  • 6

1 Answers1

1

Obviously if condition returns always true.
(example : result == result.append("Airtime: ").append(chkAirtime.isChecked()))
Why : you are using same object for if condition.

By looking at your code I think you need to check like this

if (chkAirtime.isChecked()) { //checking CheckBox is checked or not. 
    balance = balance - 50;
    result.append("Airtime: "+balance+" ");//appending data to result
}

Do the same for all other if conditions.

Important : If you want to compare two String always use equals. like s1.equals(s2); visit this : SO - Java String.equals versus ==

Community
  • 1
  • 1
Bharatesh
  • 8,943
  • 3
  • 38
  • 67