Question: I am trying to use a method to return the value of the index of an array so I can use it in another class, but I can't seem to make it work. Here is my code:

This one tells me there is no return statement.

```
public int getCourseIndex(String course){
    for (int i = 0; i<courses.length; i++)
       if(course.equals(courses[i])) 
}

```

I also tried and I think it just returns 0:

```
public int getCourseIndex(String course){
    int total = 0;
    for (int i = 0; i<courses.length; i++){
        if(course.equals(courses[i])){
            total++;
    }

    return total;
}

```

Answer: 
You can use [java.util.Arrays](http://docs.oracle.com/javase/6/docs/api/java/util/Arrays.html) as below:

```
public int getCourseIndex(String course) {
    return (Arrays.asList(courses)).indexOf(course);
}

```

Or, if you want to calculate using loop, you can:

```
public int getCourseIndex(String course) {
    for (int i = 0; i < courses.length; i++)
        if (course.equals(courses[i])) {
            return i;
        }
    return -1;
}

```
Additionally, you can use [`ArrayUtils.#indexOf(Object[] array,Object objectToFind)`](http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/ArrayUtils.html#indexOf%28java.lang.Object%5B%5D,%20java.lang.Object%29) from [`org.apache.commons.lang.ArrayUtils`](http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/ArrayUtils.html).

> 
> Finds the index of the given object in the array. This method returns
>  INDEX\_NOT\_FOUND (-1) for a null input array.
> 
> 
> 

```
public int getCourseIndex(String course){
  return ArrayUtils.indexOf(courses, course)
}

```