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;
}
int indexvariable. In your for loop, if you found acourse, set this value toiand break the loop using abreak;returnstatement is sufficient.Stringin your array. If thatStringoccurs multiple times and you want to know all those indices, you may want to return anArrayList.