I want to check if user input is equal to the array, if not toast error message when pressing a button. I am not sure if I should check input outside the button and then use an if !equals
inside button to toast the message. Here is my attempt
I have this array in strings.xml
<string-array name="people">
<item>JHON</item>
<item>MARIE</item>
<item>ALBERT</item>
<item>ALEX</item>
</string-array>
Activity.java:
String[] peopleArr =getResources().getStringArray(R.array.people);
EditText userinput=findViewById(R.id.editTextUserinput);
Button find = findViewById(R.id.findBtn);
find.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
for (int i = 0; i <= peopleArr.length - 1; i++) {
if (!userinput.getText().toString().equals(peopleArr[i])) {
Toast.makeText(getApplicationContext(), "Invalid user", Toast.LENGTH_SHORT).show();
}
}
}
This is wrong because it is toasting invalid user 4 times when the button is pressed.