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.
Asked
Active
Viewed 381 times
-5
-
5Please spend a minute reading [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) – devnull Nov 27 '13 at 06:11
-
1please share what you have tried – Cris Nov 27 '13 at 06:12
-
possible duplicate of [In Java, how can I test if an Array contains a certain value?](http://stackoverflow.com/questions/1128723/in-java-how-can-i-test-if-an-array-contains-a-certain-value) – Pankaj Kumar Nov 27 '13 at 06:12
-
@Atul Dhanuka Check out my answer. – GrIsHu Nov 27 '13 at 06:24
3 Answers
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