farissyariati
farissyariati

Reputation: 365

Un-assign EditText Value

I have some EditText in Android. I want to make a Decision:

private void makeAnOrder() {
    OrderSocket order = new OrderSocket();
    String phoneNumber = this.phoneEditText.getText().toString();
    String email = this.emailEditText.getText().toString();
    String location = this.globalLat + ", " + this.globalLon;
    String optMessage = this.optionalMessageText.getText().toString();

    if((phoneNumber == null)|| (email == null)){
        Toast.makeText(PesanTaxi.this, "Email dan No.Telepon Tidak Boleh Kosong", Toast.LENGTH_SHORT).show();
    }
    else{
        String result = order.postTaxiOrder(email, phoneNumber, location,
                optMessage);
        Toast.makeText(PesanTaxi.this, result, Toast.LENGTH_LONG).show();
    }
}

When the application is running, I let the edit text for email and phone number are blank, and pressed the button which makeAnOrder() has been assigned into that Button. However, the one which is executed is the "else" sentence.

I tried simpler one, to check it, if(phoneNumber == "") and, still it's not executed.

So, I got that, the values are neither null or "";

I wonder, how to overcome my problem.

Thank you.

Upvotes: 0

Views: 109

Answers (2)

Bobbake4
Bobbake4

Reputation: 24857

In Java when comparing strings you want to use the .equals operator to check the value. So make it like the following:

    if(phoneNumber.equals("") || email.equals("")){
            Toast.makeText(PesanTaxi.this, "Email dan No.Telepon Tidak Boleh Kosong", Toast.LENGTH_SHORT).show();
    }

Upvotes: 2

Alexander
Alexander

Reputation: 48262

Try (phoneNumber.length() < 1)

Upvotes: 0

Related Questions