1

Heads up, this is a small small part of a school project, and as such i have tried keep from asking you guys to simply write the code for me.

as a back ground: I am trying to populate this char[][] array in a spiral manner, i think i have an algorithm that will work, however, as often the case is... its not working.

But right now I am having trouble getting my code to enter and execute in this for loop. here is a small snippet...

In this specific example I am running k is a char[] array that is length 10 and pT is a char[] array of length 92

    box2 = new char[height][k.length];
    int c = 0;
    int limit = pT.length; //ensures the while loop doesint attempt to populate past the array bounds
    int w = k.length; //width of array
    int h = height; //height of array
    //these two variable i am trying to use to count how many blocks are 
    //filled on the outside of the spiral.
    //example.. as the spiral populates inward, it can no longer travel the entire
    //length of the array, but one less block for every pass
    int difH = 0; //difference in height
    int difW = 0; //difference in width
    while (limit > 0) {

        for (int n = 0; n < (w - (w - difW)); n++) {
            box[h - (h - difH)][n] = pT[c];
            limit--;
        }
    }

for whatever reason when I step through the code in netbeans it does not enter or execute this for loop, or any of the other three.

Can anyone help me figure out why my code wont enter or execute this loop?

In case you wish to see the entire while loop:

    while (limit > 0) {

        for (int n = 0; n < (w - (w - difW)); n++) {
            box[h - (h - difH)][n] = pT[c];
            limit--;
        }
        difH--;
        for (int n = h - (h - difH); n > 0; n--) {
            box[n][w - (w - difW)] = pT[c];
            limit--;
        }
        difW--;

        for (int n = w - (w - difW); n > 0; n--) {
            box[h - (h - difH)][n] = pT[c];
            limit--;
        }
        difH--;
        for (int n = 0; n < h - (h - difH); n++) {
            box[n][w - (w - difW)] = pT[c];
            limit--;
        }
        difW--;

    }

    //Prints box2
    //prints the box array
    int counter2 = 0;
    for (int column = 0; column < box2.length; column++) {
        if (column > 0) {
            System.out.print("|");
        }
        System.out.println();
        for (int r = 0; r < box2[column].length; r++) {

            System.out.print("|" + box2[column][r]);
        }
    }
    System.out.println("|\n");

If I am screwed up beyond all understandability (as is entirely possible), please just let me know and i will edit or maybe reevaluate my algorithm.

1
  • 2
    For future reference, good way to debug issues like this is just put some output printing, System.out.println(n) and so on in case of Java, so you know what is going on and can construct the logic how the program works in your head, and will see if somewhere is wrong number. Commented Feb 8, 2016 at 14:10

4 Answers 4

3

n < (w - (w - difW)) is always false:

(w - (w - difW)) = (w-w+difW) = (0+difW) = difW

but you set difW to be 0, n to be 0, and 0 < 0 is false

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

6 Comments

of course..... well thanks for the help, i guess im just having a rough morning haha
@R.Hull if that answers your question, declare it as the solution.
@fillpant We have multiple answers with the same solution
Yes, but with different timestamps :)
@Gavriel Yeah I started writing my answer before anyone had posted anything. And now there are multiple answers. The best answer may be declared as correct solution.
|
3

This:

(w - (w - difW))

evaluates to +difW which is 0. So this:

n < (w - (w - difW))

evaluates to:

n < 0

So unless n is less than zero the loop will never be entered.

Comments

2

Your problem seems to be:

int difW = 0; // this is 0
while (limit > 0) {
    // Problem is here
    for (int n = 0; n < (w - (w - difW)); n++) {
        box[h - (h - difH)][n] = pT[c];
        limit--;
    }
}

difW is 0. w is something, let us say it is 50. You have written (w - (w - difW)) what results in: (50 - (50 - 0)), what is simply 50 - 50 and equals 0. int n = 0 is the same as (w - (w - difW)). You have written ... n < (w - (w - difW) ..., so the condition is always false because of 0 < 0 is false and your for loop only gets executed if n is smaller than (w - (w - difW)) but (w - (w - difW)) is not smaller than n.

So you need to change difW to something bigger than 0.

Comments

0

In your code, difW is 0 when you enter the loop. That makes the condition be:

w - (w - 0) or (w - w) hence 0 which fails the loop test.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.