34

Given :

String[] directions = {"UP","DOWN","RIGHT","LEFT","up","down","right","left"};

String input = "Up";

How can I verify that an input from stdin is within the directions array or not ?

I can make a loop and check each item with input using equal ,but I'm looking for a more elegant way.

Regards,Ron

JAN
  • 21,236
  • 66
  • 181
  • 318
  • 1
    Did you search stackoverflow before posting the question? [link]http://stackoverflow.com/questions/1128723/in-java-how-can-i-test-if-an-array-contains-a-certain-value) – eternaln00b Feb 02 '12 at 00:19

5 Answers5

61

Convert the array of valid directions to a list:

List valid = Arrays.asList(directions)

Or just declare it directly as:

List valid = Arrays.asList("UP", "DOWN", "RIGHT", "LEFT", "up", "down", "right", "left")

You can then use the contains method:

if (valid.contains(input)) {
    // is valid
} else {
    // not valid
}

Note that this won't match a mixed case input such as "Up" so you might want to store just the uppercase values in the list and then use valid.contains(input.toUpperCase())

mikej
  • 65,295
  • 17
  • 152
  • 131
  • 4
    I'd prefer foreach on array, over creating new List just to iterate on it... – dantuch Feb 02 '12 at 00:19
  • Question for everybody - anybody have any idea why Java doesn't have an Arrays.indexOf() method? (to parallel List.indexOf()). Since the array of Strings is usually some static final thing, I find the converting to a List very annoying, so I usually write my own little linear search utility. – user949300 Feb 02 '12 at 00:22
  • @user949300 - use ArrayList() – Travis J Feb 02 '12 at 00:32
  • Mostly because generics and arrays don't get along. Avoid using arrays, use `List`. – Louis Wasserman Feb 02 '12 at 02:25
  • Does not work. I created a 2D array in which every element is "EMPTY". `Arrays.asList(array).contains("EMPTY")` returns true until one element changes to something that isn't "EMPTY". Why? The array still contains "EMPTY". – Erik Humphrey May 24 '17 at 17:30
  • @ErikHumphrey if it's a 2D array then does it contain an array that contains "EMPTY" rather than just "EMPTY" itself? If you're still having trouble then post a fresh question with an example of your code. – mikej May 24 '17 at 17:38
  • @mikej What do you mean? Every element in all of the arrays is a string "EMPTY". – Erik Humphrey May 24 '17 at 17:50
  • If you have an array of arrays and use `asList` then you'll get a `List` of arrays, so a `contains` check for a string won't match. Anyway, sounds like your scenario is different to the original question here. If you need a hand, post a fresh question with code demonstrating the problem. Thanks. – mikej May 24 '17 at 18:15
  • @user949300 Java has a `Arrays.binarySearch`, but while this expects the array to be sorted, it is far quicker with larger array sizes – Ferrybig Oct 23 '17 at 10:53
6

Convert your array to a List and than use the contains method.

List mylist = Arrays.asList(directions);
mylist.contains(input);

The contains method returns:

true if the list contains the specified element.

RanRag
  • 48,359
  • 38
  • 114
  • 167
3

Use ArrayList instead and its contains method

Ry-
  • 218,210
  • 55
  • 464
  • 476
wmz
  • 3,645
  • 1
  • 14
  • 22
3

Unfortunately, Java does not have an Arrays.indexOf() method. Your best bet is to write a little utility to do a simple linear search. Or you could convert to an ArrayList (see Arrays.asList()) and call indexOf() or contains().

If the array is large and speed is a concern, you could sort the array, and then use Arrays.binarySearch().

user949300
  • 15,364
  • 7
  • 35
  • 66
0

Since we are specifically talking about arrays of strings, not just any array: another approach which solves the case-insensitivity question elegantly would be using regular expressions. Either through the Pattern class or the String's matches method.

if (input.matches("(?i)" + String.join("|", directions))) {
    // valid
}
else {
    // invalid
}
cviejo
  • 4,388
  • 19
  • 30