0

Can anyone explain how it actually works? ive been carefully checking every single step with my note and all written down, and it does work fine, but why is the spot 4 all the time?when i init it with 0, it doesnt make sense to me...

here is the output enter image description here

    Random rand = new Random();
    int[][] map = new int[row][column];
    int spot = 0;
    int i=0;
    int j=0;

    /*ignore this part
    for(i = 0; i<map.length; i++){
        for(j = 0; j<map[i].length; j++) {
            map[i][j] = rand.nextInt(2);
        }
        System.out.println();
    }*/

    System.out.println();

    for(i =0; i<map.length; i++){
        for(j =0; j<map[i].length; j++){
            if(map[i][j] == 1){
                while(spot<4){
                    map[i][j] = 5;
                    spot++;
                }
            }

            System.out.print(map[i][j]+" ");

        }
        System.out.print(spot);
        System.out.println();
    }


    System.out.println();

}
6
  • what? i dont understand... Commented Nov 7, 2019 at 11:39
  • 2
    Could you please post your initilization code - everything relevant before the loop? Commented Nov 7, 2019 at 11:40
  • thomas, okay sorry, i just edited with the full code of method @Guy, i did, if i remove it, it only doesnt print out the spot, which you can see the output i posted Commented Nov 7, 2019 at 11:43
  • 1
    @VerzChan try to use the debugger. Set a whole bunch of breakpoints, run the debugger and see what is happening at each step. Gradually, you'll come to understand which breakpoints to remove and then remove them then debug again and proceed. You will get the explanation by yourself. Commented Nov 7, 2019 at 11:47
  • 1
    Remove the while loop in the innermost for loop so that you only increase spot once, then I am not sure what you want to do. Only have max 4 values replaced with 5 or only one 5 per row or... Commented Nov 7, 2019 at 12:00

2 Answers 2

1

This line:

System.out.print(spot);

Your array has only 4 members, but in printing the value of "spot" at the end of each line, you're printing 4 at the end of each line.

Edit: responding to your comment about increasing 'spot' only when a 1 is found:

    if(map[i][j] == 1){
            while(spot<4){ // <<<< you increase spot until it equals 4
                map[i][j] = 5;
                spot++;
            }
        }

If your intent is to count the number of occurrences of '1', you'll want to remove that nested while:

    if(map[i][j] == 1){
            map[i][j] = 5;
            spot++;
        }
Sign up to request clarification or add additional context in comments.

4 Comments

Umm, but in the code, i make the spot++, only if it finds "1" in the 2d array element which is map[i][j], as you can see, when it finds that element, it will be stored as 5 instead, and the output can only find 1 of those 5 before the spot becomes <4, and thats it...
Please see my edit. You're increasing until it equals 4. And each of your 'rows' contains a 1 somewhere, so you'll always end up with spot==4?
Okay, let me ask a question, in my while loop, does it stay inside the whole time, until the spot becomes 4? and then it gets out of that loop to do the next index of for?
Yes, the while loop will be exceuted to its completion. Which also means that after the first time of entering that loop, spot will always ==4, so the same while loop will never be entered again, even if another 1 is found. Do you have access to a step debugger (such as in any half decent IDE)? You could use that to debug and step through every single line, seeing all the values at that point of execution.
1

I'm not sure what you are trying to do with spot, but this is you problem. The first time if(map[i][j] == 1) is true you are increasing spot to 4, so while(spot<4) won't be executed again. If you remove it it will work.

if(map[i][j] == 1){
    map[i][j] = 5;
}

Edit

Base on the comments, if you want to replace only 4 1 you can extract the replacing to a method and return as soon as you swapped 4 1

public void replaceOnes(int[][] map) {
    int spot = 0;
    for(int i = 0 ; i < map.length ; i++){
        for(int j = 0 ; j < map[i].length ; j++){
            if(map[i][j] == 1){
                map[i][j] = 5;
                spot++;
                if (spot == 4) {
                    return;
                }
            }
        }
    }
}

replaceOnes(map);

for(i = 0; i<map.length; i++){
     for(j = 0; j < map[i].length; j++){
        System.out.print(map[i][j]+" ");
     }
     System.out.println();
}

7 Comments

I may have found the problem, but let me ask a question, in my while loop, does it stay inside the whole time, until the spot becomes 4? and then it gets out of that loop to do the next index of for?
This will replace all 1's with 5's, is that really correct?
yeah, but the things is, i only want 4 of those 1s become 5, not all the 1s, so thats why i put while loop inside, maybe i can put something like if(map[i][j] == 1 && spot<4) and increment the spot inside.
@VerzChan it will do map[i][j] = 5; 4 times, but on the same i and j.
@Guy, sorry for late response, not it wont be on the same i and j, it will be on the same element, but different index obviously, i tried it and it worked, the whole problem from my post is that, i misunderstood while loop as an if, not as a loop, so i thought it doesnt stay inside the loop while in true condition
|

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.