-2

I'm trying to get an if statement to test for null values to work, but it my app just seems to ignore it. below is my current code (woreks perfectly aside from the second if statement.

public void stuff(View v){

    the_problem_string = "";


    //this one works
    if (another_string == null){
    please_wait_for_value_msg = Toast.makeText(getApplicationContext(), "Please wait for values", Toast.LENGTH_SHORT);
    please_wait_for_value_msg.setGravity(Gravity.TOP|Gravity.CENTER, 0, 250);
    please_wait_for_value_msg.show();
    return;
    }

    AlertDialog.Builder save = new AlertDialog.Builder(this);
    save.setTitle("Save Location");
    save.setMessage("Enter description");
    final EditText input = new EditText (this);
    save.setView(input);
    save.setPositiveButton("Ok", new DialogInterface.OnClickListener(){

            public void onClick(DialogInterface dialog, int whichButton) {


              Editable value = input.getText();
              String something = value.toString();
              the_problem_string = something;
              //this is what is not working
              if (the_problem_string == null || the_problem_string == ""){

                  please_enter_description_msg = Toast.makeText(getApplicationContext(), "Please enter a description", Toast.LENGTH_SHORT);
                  please_enter_description_msg.setGravity(Gravity.TOP|Gravity.CENTER, 0, 250);
                  please_enter_description_msg.show();
                    return;

              }do a lot more stuff

}
                });
       save.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int whichButton) {
                return;
              }
            });
            save.show();
    }

The problem I'm having is with that second if statement. I need it to not "do a lot more stuff" if the_problem_string is null (or otherwise has no entered value). I'm sure its just something small I'm overlooking, but can someone point me in the right direction or at least help understand why I'm not getting the desired results?

Just_Some_Guy
  • 330
  • 5
  • 24
  • possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – takendarkk Aug 26 '14 at 22:05

4 Answers4

1
the_problem_string == "" 

is not how you compare strings in java

you should use

the_problem_string.equals("")
BrainCrash
  • 12,992
  • 3
  • 32
  • 38
0

You could do:

the_problem_string == null || the_problem_string == ""

with:

TextUtils.isEmpty(the_problem_string)
pomber
  • 23,132
  • 10
  • 81
  • 94
0

For string comparisons in Java, you must use the equals() method, not the == operator. So instead of the_problem_string == "" try the_problem_string.equals("").

This is because String is a class in Java, and the implementation of the == operator for objects is a reference comparison.

jcmanke
  • 56
  • 1
  • 4
0

This the_problem_string = ""; along with this

if (the_problem_string == null || the_problem_string == ""){

is not doing what you think it is doing. Something like the following should work

if (the_problem_string == null || the_problem_string.length() == 0){

this the_problem_string == "" is not a good string comparison.

jch
  • 3,600
  • 1
  • 15
  • 17