0

I am trying to create an app that checks the condition of a statement entered in an EditText field. At the moment, I have written if the word entered is 'next' which is not case sensitive to then proceed onto the next screen by pressing the 'Blogin' button.

My question is how do I write an 'if' statement so that if a phrase is entered into the EditText field and just any part of the phrase contains the word 'next' to then proceed to the next page by pressing the 'Blogin' button? For example if a phrase is entered as 'please go to the next page', the 'if' statement should recognise there is the word 'next' which shouldn't be case sensitive and hence allow you to proceed to the next page by pressing the 'Blogin' button.

Below is a snippet of part of the relevant code that needs to be changed:

 public void onButtonClick(View v) {
    if (v.getId() == R.id.Blogin) {
        String str = a.getText().toString();


        //Go to the next 'Display' window or activity if the person enters the correct username which is not case sensitive
        if (str.equalsIgnoreCase("next")) {
            Intent userintent = new Intent(MainActivity.this, Display.class);
            startActivity(userintent);
        } else {
            Toast.makeText(getApplicationContext(), "Incorrect Information", Toast.LENGTH_SHORT).show();
        }
    }
}
N MC
  • 207
  • 1
  • 10

2 Answers2

2

All you really need to do is use the contains() method in conjunction with the toLowerCase() method:

    if (str.toLowerCase().contains("next")) {
        Intent userintent = new Intent(MainActivity.this, Display.class);
        startActivity(userintent);
    } else {
        Toast.makeText(getApplicationContext(), "Incorrect Information", Toast.LENGTH_SHORT).show();
    }

For more in-depth info, see here: String contains - ignore case

Community
  • 1
  • 1
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
1

You can use java.lang.String.contains(), like this:

if (str.contains("next")||str.contains("Next") {
    Intent userintent = new Intent(MainActivity.this, Display.class);
    startActivity(userintent);
} else {
    Toast.makeText(getApplicationContext(), "Incorrect Information", Toast.LENGTH_SHORT).show();
}
Matei Radu
  • 2,038
  • 3
  • 28
  • 45