0

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;
}
5
  • The easiest way would be to create an int index variable. In your for loop, if you found a course, set this value to i and break the loop using a break; Commented Dec 10, 2013 at 8:05
  • Does it ever enter the if block in the second code snipet ? Commented Dec 10, 2013 at 8:06
  • could you show me what you mean about the break? Commented Dec 10, 2013 at 8:07
  • @user2848565 You can read it here : docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html. Additionally, like answers have mentionned, if you only need to return the index, a return statement is sufficient. Commented Dec 10, 2013 at 8:08
  • @user2848565 Don't forget that with your approach & the others suggested you only get the index of the first occurrence of the String in your array. If that String occurs multiple times and you want to know all those indices, you may want to return an ArrayList. Commented Dec 10, 2013 at 8:17

4 Answers 4

3

You need to go through the array with a for loop and if you find what you were looking for, return the current loop variable value (in the following code the i value), which actually represents the index in the array on which it was found.

If the loop ends and nothing gets returned, it means that what you were looking for wasn't in the array. Then you should return something to inform you about that fact. It needs to be something that can't get returned from inside the for loop, which are negative numbers (typically a -1).

public int getCourseIndex(String course){
    for (int i = 0; i<courses.length; i++){
        if(course.equals(courses[i])){
            return i;
        }
    }
    return -1; // not found
}
Sign up to request clarification or add additional context in comments.

2 Comments

There's no closing brace for the for loop:D
@user16547: Oh, thank you. But to defend myself, he didn't have it in the question either. ;)
2

You can use java.util.Arrays 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;
}

1 Comment

I like this answer. Exposes the OP to Arrays - a useful look-ahead.
1

Additionally, you can use ArrayUtils.#indexOf(Object[] array,Object objectToFind) from org.apache.commons.lang.ArrayUtils.

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.