-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
  • Instead of == use this Commented Jul 15, 2016 at 7:49
  • set the hei=""; and check with hei.equalsIgnoreCase("") Commented Jul 15, 2016 at 7:49
  • see my updated answer Commented Jul 15, 2016 at 7:50

3 Answers 3

1

You can also try this like

if (hei == null || wei == null || hei.equals("") || wei.equals("")){ // your body here } 
Sign up to request clarification or add additional context in comments.

Comments

1
 Try this check if(!TextUtils.isEmpty(string){ //body here } 

1 Comment

How can string be both null and equal to ""?
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 

2 Comments

getLength doesn't exist
Ouch, error. Thank you. I edited my answer...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.