-1

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;
}
Rory O'Kane
  • 29,210
  • 11
  • 96
  • 131
Anya
  • 7
  • 1

1 Answers1

3

The cause of your problem is the Java concept of variable “scope”, specifically “block scope”. When a variable is defined, it only exists within the block ({ }) it was defined in. In code that runs after the block, the variable has been thrown away and can no longer be used. Java does this both to reduce memory used by programs and to make code simpler to understand – when reading the code, you know you don’t have to consider variables that are no longer in scope.

Block scope causes your problem in this part of the code:

        for (int e = first; e < strArray.length; e += 1) {
          int second = Arrays.asList(strArray).indexOf(str);
        }

        return second;

Your second variable does not exist on the line return second. The previous int second variable declaration was only valid for the block that declaration was made in – the block of the for loop.

To fix this, you have to declare the variable in the outermost scope it is used. Though your code can’t know what value the second variable should have before the for loop runs, you can still declare the variable without assigning a value, then assign the value on a later line:

        int second;
        for (int e = first; e < strArray.length; e += 1) {
          second = Arrays.asList(strArray).indexOf(str);
        }

        return second;
Rory O'Kane
  • 29,210
  • 11
  • 96
  • 131
  • I tried this and it says 'error: variable second might not have been initialized', I feel like either indentation is messed up, either curly braces or my overall logic – Anya May 29 '21 at 02:48
  • @Anya No, your indentation and curly braces look fine. To initialize a variable means to set its value for the first time. In my suggested code, the variable `second` is initialized on the `second = …;` line in the `for` loop. The error message correctly warns that it’s possible for that line to never run, so `return second` would return nothing. The initialization line could be skipped if the `for` loop runs 0 times, which could happen if `!(first < strArray.length)`. – Rory O'Kane May 29 '21 at 04:44
  • @Anya One possible solution is described in this question: [Java: Error: variable might not have been initialized](https://stackoverflow.com/q/24152351/578288). In general, you’ll save time if you try searching Google and Stack Overflow for your error message and see if there are any existing pages about it. Question-answerers on Stack Overflow generally appreciate if you show effort by linking to the related pages you found and describing why they didn’t help. – Rory O'Kane May 29 '21 at 04:46