-5

I have a string array. Now I prompt the user for a string and then I need to check if that string is present in the string array or not. Someone please help me do this.

Atul Dhanuka
  • 1,453
  • 5
  • 20
  • 56

3 Answers3

1

This will solve your problem.

 if(check(edittext.getText.toString())){
//do somethingif true
}else{
//else
}

public boolean check(String text){
for(String s: text){
if(s.equalsIngoreCase(text)){
return true;
  }
}
return false;
}
Aishvarya Jaiswal
  • 1,793
  • 6
  • 34
  • 72
0

Try this:

String[] ordinals = {"donut", "eclair", "froyo", "gingerbread"};
String mystr="donut";
  for (String ord : ordinals) {
     if (mystr.contains(ord)) {
         System.out.println(mystr);
        break;
     }
   }
GrIsHu
  • 29,068
  • 10
  • 64
  • 102
0

Try this:

    String[] ar = {"abc","xyz","pqr"};//Input array
    Arrays.sort(ar);
    String key = "abc";//Your key
    if(Arrays.binarySearch(ar, key) >= 0) {
        return true;//Key found
    }

It is useful if array size is big

Chandrayya G K
  • 8,719
  • 5
  • 40
  • 68