4

I know I'm making an error somewhere in this code, but I can't figure it out. The player1.getId(); returns a value of 1 just so you are aware. I'm trying to print the index of the array where the value is 1. At the end of the code, I expected currentX to be 0 and currentY to be 0, but they were both 9. Any help would be super.

int[][] grid = {
    {3, 3, 3, 3, 3, 3, 3, 3, 3, 3},
    {3, 3, 3, 3, 3, 3, 3, 3, 3, 3},
    {3, 3, 3, 3, 3, 3, 3, 3, 3, 3},
    {3, 3, 3, 3, 3, 3, 3, 3, 3, 3},
    {3, 3, 3, 3, 3, 3, 3, 3, 3, 3},
    {3, 3, 3, 3, 3, 3, 3, 3, 3, 3},
    {3, 3, 3, 3, 3, 3, 3, 3, 3, 3},
    {3, 3, 3, 3, 3, 3, 3, 3, 3, 3},
    {3, 3, 3, 3, 3, 3, 3, 3, 3, 3},
    {3, 3, 3, 3, 3, 3, 3, 3, 3, 3}
};

int currentX = 0;
int currentY = 0;

grid[0][0] = player1.getId();

grid[0][9] = 2;

for (int i = 0; i < grid.length; i++) {
    for (int j = 0; j < grid[0].length; j++) {
        if (grid[i][j] == player1.getId());
        {
            currentX = i;
            currentY = j;
        }
        System.out.print(grid[i][j]);
    }

}
System.out.println();
System.out.println("Player1 is currently in row " + currentX + " and column " + currentY);
1
  • are you sure you want grid[0].length in your inner for, seems like you are after grid[i].length Commented Sep 29, 2015 at 15:54

2 Answers 2

6

Remove the semicolon(;) at the end of if (grid[i][j] == player1.getId());

Consider how works if statement of java

The if statement of java executes it's block code if the expression of if statement is true. Semi colon ends a statement of java. If you put empty semi colon after if statement it counts as an empty statement. So, if statement does nothing when executing if statement which having semi colon at the end. Java compiler compiles your code similarly as follows.

if (grid[i][j] == player1.getId()){
    //nothing here
}

{
    currentX = i;
    currentY = j;
}

See what happens when other kind statement had semi colon at the end.

  • while loop

        while (expression);
        {
            //something goes here 
        }
    

The condition can be true or false when initializing while loop. If the condition is true it makes an infinite loop. Nothing will execute after the line. If expression is false, it executes once the expected content of while loop.

  • switch (integer); and catch (Exception e);

It fails to compile and getting an exception { expected

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

3 Comments

I'll just add that you can prevent this kind of mistake in the future by putting opening braces on the same line
Thank you so much for this. I am embarrassed by this oversight.
Train to put open braces, then you will not get this bug. This kind of bug are hard to identify when it is in a complex code.
1

The condition is true here (if player1.getId() == 1):

if(grid[i][j] == player1.getId());

But the code contains a logical error: a group of operators here - the empty operator ; and it will be executed...

currentX and currentY always will be equals the length of the array.

currentX = grid.length;
currentY = grid[0].length;

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.