I am writing a method that looks for the index of the second occurrence of a substring in an array. It finds the index of the first occurrence fine.
Next, I tried to create the loop that would start from the first
index, but it didn't work. It says 'error: cannot find symbol' about my second
variable.
Please give me any advice how to improve my code or to use some other implementation of this method…
int findSecond(String[] strArray, String str) {
for (int i = 0; i < strArray.length; i += 1) {
if (i > 1) {
int first = Arrays.asList(strArray).indexOf(str);
// int second = Arrays.asList(strArray).indexOf(str, first + 1);
for (int e = first; e < strArray.length; e += 1) {
int second = Arrays.asList(strArray).indexOf(str);
}
return second;
}
}
return -1;
}