59

OK let's say I have an array filled with {"tube", "are", "fun"} and then I have a JTextField and if I type either one of those commands to do something and if NOT to get like a message saying "Command not found".

I tried looking in Java docs but all I am getting is things that I don't want like questions and stuff... so, how is this done? I know there is a "in array" function but I'm not too good with combining the two together.

Thanks.

Here is what I have so far:

String[] dan = {"Red", "Orange", "Yellow", "Green", "Blue", "Violet", "Orange", "Blue"};
boolean contains = dan.contains(say.getText());

but I am getting cannot find symbol in dan.contains

Pablo Fernandez
106k59 gold badges195 silver badges234 bronze badges
asked Aug 26, 2010 at 3:59
0

5 Answers 5

82

This is what you're looking for:

List<String> dan = Arrays.asList("Red", "Orange", "Yellow", "Green", "Blue", "Violet", "Orange", "Blue");
boolean contains = dan.contains(say.getText());

If you have a list of not repeated values, prefer using a Set<String> which has the same contains method

answered Aug 26, 2010 at 4:04
3
  • That's because an array is not a set Commented Aug 26, 2010 at 4:09
  • @Dan - BZZZT - you are trying to invoke contains on an array, not a Set!!! Commented Aug 26, 2010 at 4:10
  • THANKS SIMPSO- I MEAN PABLO! :D Tahnks Stephen. Rookie mistake. Commented Aug 26, 2010 at 4:59
40
String[] a= {"tube", "are", "fun"};
Arrays.asList(a).contains("any");
answered Aug 26, 2010 at 4:08
1
  • THIS is what I was looking for. Simple ! Commented May 19, 2015 at 18:37
7

Use Arrays.asList() to wrap the array in a List<String>, which does have a contains() method:

Arrays.asList(dan).contains(say.getText())
answered Aug 26, 2010 at 4:08
6

This can be done in java 8 using Stream.

import java.util.stream.Stream;
String[] stringList = {"Red", "Orange", "Yellow", "Green", "Blue", "Violet", "Orange", "Blue"};
boolean contains = Stream.of(stringList).anyMatch(x -> x.equals(say.getText());
answered Jan 6, 2017 at 19:01
1
  • 1
    This is the best answer. The others fail because asList returns non-parameterized generic. Commented Jan 23, 2018 at 18:13
1

If you can organize the values in the array in sorted order, then you can use Arrays.binarySearch(). Otherwise you'll have to write a loop and to a linear search. If you plan to have a large (more than a few dozen) strings in the array, consider using a Set instead.

answered Aug 26, 2010 at 4:06
3
  • Erm, would it matter if I said that the array would be permnant and not changed at a order at all? Would there be an easier way than? Commented Aug 26, 2010 at 4:09
  • @Dan - I think his first sentence answers that. You do know how to sort strings by hand, don't you? Commented Aug 26, 2010 at 4:14
  • 1
    @Jim, this answer is incorrect. The documentation of #binarySearch explicitly states: "If the array contains elements that are not mutually comparable (for example, strings and integers), it cannot be sorted according to the natural ordering of its elements, hence results are undefined." DO NOT use binarySearch on Integer or String, or any elements that can at most have a partial order. Commented Jan 18, 2017 at 7:54

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.