If condition is ignored and it always jumps to else statement in android

4107 views java

I am a beginner in android and this might be something pretty easy but i cant figure it out

public void login (View view){
    EditText et = (EditText) findViewById(R.id.txtUserName);
    String text= et.getText().toString();
    System.out.println("text = "+text);

        System.out.println("Im in if");
        Intent intent = new Intent(this, Order.class);

    }else if(text.matches("HOD")){

        Intent intent = new Intent(this,HOD.class);

    }else if(text.matches("HR")) {

        Intent intent = new Intent(this,HR.class);


        System.out.println("Im in else");



the if statement doesn't work and it always jumps to the else statement

answered question

Debug your code and check the value of text. And why are you using matches? why not equals or contains? you don't need regex for that.

use equals instead of matches

You may also want to trim your text

use equal or equalsIgnoreCase

1 Answer


The method matches() expects a regex as a parameter. But you want to check if the Strings are the same. So you should use if(text.equals("")) instead of matches("").

posted this

Have an answer?


Please login first before posting an answer.