Java function local variable cannot be resolved

You are missing the { and } in the inner for-loop so the scope of the loop ends when the first ; is found. Then: j is not present in the if condition…

public static int[] doSelectionSort(int[] arr) {

    for (int i = 0; i < arr.length - 1; i++) {
        int index = i;

        for (int j = i + 1; j < arr.length; j++) {
        //                                       ↑  here!
            System.out.println(arr[index]);
            if (arr[j] < arr[index])
                index = j;
        }
    //  ↑  here!

        int smallerNumber = arr[index];
        arr[index] = arr[i];
        arr[i] = smallerNumber;
    }
    return arr;
}

Leave a Comment