#Variable cannot be defined

1 messages · Page 1 of 1 (latest)

burnt quail
#
public static int wrongNumber(int[][] m) {
    for (int i=0;i<4;i++) {
    if (m[i][3] != m[i][0] + m[i][1] + m[i][2] + m[i][3]) {
    int badrow = i;                                        //BADROW DEFINED
    break;
    }
    }
        
    for (int i=0;i<4;i++) {
        if (m[3][i] != m[2][i] + m[3][1] + m[3][0]) {
    int badcolumn = i;                                    //BADCOLUMN DEFINED
    break;
    }
    }
        
    int correctValue = (m[badrow][3]-m[badrow][2]-m[badrow][1]-m[badrow][0]) + m[badrow][badcolumn]; //ERROR HERE

    return correctValue;    
}```

The code above is used to take a 2D array that is 'incorrect' as input and output an integer that can be used to correct the array. 
https://edabit.com/challenge/fEtBbju4zCmYKFAwH It is for this code problem. 

I am getting the following errors:

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
badrow cannot be resolved to a variable
badcolumn cannot be resolved to a variable```

What mistake have I made here? I defined the variables badcolumn and badrow in the function. My IDE is highlighting the following line of code:

int correctValue = (m[badrow][3]-m[badrow][2]-m[badrow][1]-m[badrow][0]) + m[badrow][badcolumn];```
brazen juncoBOT
#

<@&987246399047479336> please have a look, thanks.

brazen juncoBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#

The variable badrow is defined within the if statement block. This means that it can only be accessed within that block and cannot be used outside of it. If you try to access badrow outside of the if statement, you will get a compilation error. To fix this, you can declare the badrow variable before the if statement block, like this:

public static int wrongNumber(int[][] m) {
    int badrow = -1; // initialize with a default value
    for (int i=0;i<4;i++) {
        if (m[i][3] != m[i][0] + m[i][1] + m[i][2] + m[i][3]) {
            badrow = i;
            break; // exit the loop once a bad row is found
        }
    }
    return badrow;
}

In this updated code, badrow is declared before the for loop and given an initial value of -1. If a bad row is found, its index is assigned to badrow and the loop is exited using the break statement. Finally, badrow is returned as the result of the method.

fluid gate
#

ChatGPT has the correct answer again ^-^

burnt quail
#

oh if statement have their own scope?

fluid gate
#

yessir

#

everything with the curly brackets is a scope

burnt quail