0
    for (i=1; i < 9; i++) {
        for (j=1; j < 9; j++) {
            if ( board[i][j] == "o" ) {
                j = j-1;

                if ( board[i][j] == "x" ) {
                    do {
                        j--;
                    }
                    while (board[i][j] != "-");
                    board[i][j] = ".";
                }
            }
        }
    }

I have this piece of code as part of a method, there are two versions, one as written above and one where j = j-1 is replaced with j = j+1 and j-- is replaced with j++

The positive version works perfectly fine but if I put in the negative version, the code compiles fine but when I attempt to run it nothing happens, the console just hangs and I have to close and reopen it. Can anyone tell me what I am doing wrong? Thanks in advance.

edit:

for (i=1; i < 9; i++) {
        for (j=1; j < 9; j++) {
            if ( board[i][j].equals("o") ) {
                j = j-1;

                if ( board[i][j].equals("x") ) {
                    do {
                        j--;
                    }
                    while (!board[i][j].equals("-"));
                    board[i][j] = ".";
                }
            }
        }
    }

I have edited the code as indicated but the problem persists

Haruk20
  • 41
  • 5
  • @tunaki I'm not sure how this is a duplicate of the post you indicated? – Haruk20 Feb 14 '16 at 21:51
  • 3
    Look at this again `if ( board[i][j] == "o" )` – Tunaki Feb 14 '16 at 21:55
  • but this part works perfectly fine when i have j = j+1, why should it stop working when i say j=j-1? – Haruk20 Feb 14 '16 at 21:56
  • @Tunaki I'm not having trouble comparing anything, the problem arises when I try to set my integer j to j-1 – Haruk20 Feb 14 '16 at 22:04
  • 1
    Your code as-is is incorrect. And this is probably related to comparing Strings. You need to fix this and debug. – Tunaki Feb 14 '16 at 22:05
  • @Tunaki I edited the code as the other post recommended but my problem persists – Haruk20 Feb 14 '16 at 22:14
  • 1
    You also have a trailing `;` here `while (!board[i][j].equals("-"));`. I suggest you restart and try to make a [mcve]. Please read [ask]. – Tunaki Feb 14 '16 at 22:16
  • You also never check board[0][0] is that intentional? Arrays are zero indexed – OneCricketeer Feb 14 '16 at 22:21
  • 1
    @Tunaki - do while loops have trailing semi colons – OneCricketeer Feb 14 '16 at 22:22
  • @Tunaki That trailing `;` is part of a do/while. It isn't incorrect. – user207421 Feb 14 '16 at 22:22
  • 1
    And changing the loop variable within the loop doesn't seem like a good idea... – OneCricketeer Feb 14 '16 at 22:23
  • @cricket_007 yes it's intentional, the first row and column are just numbered coordinates, so I don't want to check them – Haruk20 Feb 14 '16 at 22:23
  • @EJP Why was this posted reopened? It is still obviously missing a [mcve]! We still have no idea what the problem is after 10 comments at least. – Tunaki Feb 14 '16 at 22:24
  • 1
    If the console is "hanging" that most likely (almost certainly) means you have created an infinite loop. Have you tried stepping through in a step by step debugger (as will be provided in any good IDE) to examine the code flow – Richard Tingle Feb 14 '16 at 22:25
  • 1
    I guess the fact that your program hangs has something to do with `j = j - 1` and the `j++` in your `for`-loop. You should avoid modifying the `for` loop counter variable (`i` and `j`), unless you absolutely have to or know what you're doing. – MC Emperor Feb 14 '16 at 22:25
  • @Tunaki Because it isn't a duplicate of what it was closed as? At least three of those comments are about an irrelevance. – user207421 Feb 14 '16 at 22:29
  • @EJP So you want to close it again as "No MCVE"? – Tunaki Feb 14 '16 at 22:29
  • @Tunaki I don't want to close it at all. Why are you asking me? – user207421 Feb 14 '16 at 22:30
  • 2
    Thanks cricket_007 MCEmperor and RichardTingle this was the problem, very silly mistake on my part thanks for the help – Haruk20 Feb 14 '16 at 22:32
  • Resolved in a manner not to be useful to future readers (allegedly). Wanna just delete it? That is, only if *you* want to – Drew Feb 14 '16 at 22:39
  • @Drew , If you're asking me, I have no issue with it being deleted if it won't be useful to future readers. – Haruk20 Feb 14 '16 at 22:45
  • I am not the judge on that. You choose. Maybe those that follow will get something out of it. Thanks. – Drew Feb 14 '16 at 22:46
  • @Drew Either you are suggesting this is not useful or you aren't. Make up your mind. – user207421 Feb 14 '16 at 22:47
  • @EJP I just answered that. Once it hits 2 answers, deleting is more difficult. – Drew Feb 14 '16 at 22:48
  • 1
    @Drew I can't see any harm in leaving it on the off chance that someone makes the same mistake as I did. – Haruk20 Feb 14 '16 at 22:48
  • @Drew So who is doing the alleging here? Can't see anyone else doing it. – user207421 Feb 14 '16 at 22:48
  • Back to what we were doing – Drew Feb 14 '16 at 22:49
  • @Drew Please clarify your comments as requested. Or delete them. They appear to be quite devoid of meaning to me. – user207421 Feb 14 '16 at 22:51
  • @EJP There is a close reason for Typo, or resolved in a manner unlikely to be useful to future readers. If people want to close or leave open, fine with me. I was alerting the OP of the options. Thanks. Now I *do* have to split. – Drew Feb 14 '16 at 23:03

1 Answers1

1

Your logic is wrong. You're never changing the square that contains "x", so you keep encountering it, so you keep decrementing j, so you will encounter it again next time, so ...

user207421
  • 305,947
  • 44
  • 307
  • 483
  • you're right I was attempting to find the space next to the one with the "x" but didn't realise i was creating the infinite loop by using the same variables in my a for loop and my while loop. Thanks – Haruk20 Feb 14 '16 at 22:41