-1

I got this problem where I have three if statements and every one of them has IF String == null or String == "" and even the getText doesn't give any value, these doesn't activate.

 @Override
public boolean onOptionsItemSelected(MenuItem item) {


    switch (item.getItemId()) {

        case R.id.action_two:

            final String hei = h.getText().toString();
            final String wei = w.getText().toString();
            String waist = wc.getText().toString();


            if (hei == null || wei == null ){
                final Animation animationFadeIn = AnimationUtils.loadAnimation(this, R.anim.fadein);
                checker.setText("Please, put your information in these spots!");
                checker.startAnimation(animationFadeIn);



            }

            else if (waist == null){

                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("Are you sure?");
                builder.setMessage("If you let Waist circumference empty, we will ask it from you later.")
                        .setCancelable(true)
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {

                                Intent i = new Intent(getApplicationContext(), number3.class);
                                i.putExtra("height" , hei);
                                i.putExtra("weight" , wei);
                                startActivity(i);
                                overridePendingTransition(R.anim.slide_in, R.anim.slide_out);

                            }


                        });
                AlertDialog alert = builder.create();
                alert.show();




            }

            else{

                Log.d("BMI Height" , String.valueOf(wei));
                Log.d("BMI Weight" , String.valueOf(hei));
                Intent i = new Intent(getApplicationContext(), healthcalculator3.class);
                i.putExtra("height" , hei);
                i.putExtra("WC" , waist);
                i.putExtra("weight" , wei);
                startActivity(i);
                overridePendingTransition(R.anim.slide_in, R.anim.slide_out);


            }

And even if all those (waist, wei, hei) are null or not value set, it only activates the last ELSE and the machine thinks that they have some data/value.

3 Answers3

1
    Try this check

    if(!TextUtils.isEmpty(string){
       //body here
    }
Khizar Hayat
  • 3,427
  • 3
  • 18
  • 22
1

You can also try this like

if (hei == null || wei == null || hei.equals("") || wei.equals("")){
     // your body here
}
AMAN SINGH
  • 3,491
  • 6
  • 28
  • 44
0

The best way to compare strings is use stringInstance.equals("what to compare"), so your code will be:

hei.equals("")

Also you can use length of string:

hei.length() > 0
HormCodes
  • 89
  • 6