0

I'm very new to java and am trying to use if statements do decide which variables are in my array.

if (count<2){
                int blockA = arrayOne[x-1][y];
                int blockB = arrayOne[x-1][y];
                int blockC = arrayOne[x][y-1];
                int blockD = arrayOne[x][y+1];

                int[] checker = {blockA,blockB,blockC,
                    blockD};
                checkCount = 4;
            }
            else {
                int blockE = arrayOne[x+1][y];
                int blockF = arrayOne[x-1][y];
                int blockG = arrayOne[x][y+1];

                int[] checker = {blockE,blockF,blockG};
                checkCount = 3;
            }

but every time I compile it comes up saying "cannot find symbol" and is talking about when I use the checker array later on, arrayOne contains only numbers. Is there a way to make this work without using a funciton? Cheers

1 Answer 1

3

You're declaring the array in the scope of the if or else statement.

When the closing } is reached that scope no longer exists, thus the variable is no longer available.

Declare the variable outside of the if statement and initialize it in the if statement.

Care must be taken to ensure it's initialized for all execution paths.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.