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;
}

```
To return the index you should traverse the loop and return the `i` when your validation results `true`.

If not found you have two option, return `-1` or throw an exception. 

```
public int getCourseIndex(String course){

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

```