public class StringArray {
private String strArr[];
public StringArray(int capacity) {
strArr = new String [capacity];
}
public int indexOf(String s) throws StringNotFoundException {
for(int i=0;i<strArr.length ;++i) {
if (strArr[i].equals(s)) {
return i;
} else {
throw new StringNotFoundException();
}
}
}
}
What I want to do is to return the index of the string I'm looking for if it's in the array, to throw an exception otherwise.
However Eclipse says I've got to return an int.
So should I just change the return type to void or are there other options?
StringNotFoundException is a custom exception I've made up.
do like this