R.Hull February 2016

JAVA not entering for loop

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--;
              

Answers


Gavriel February 2016

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


DaveRlz February 2016

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.


Wayne Tanner February 2016

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.


xdevs23 February 2016

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.

Post Status

Asked in February 2016
Viewed 2,021 times
Voted 10
Answered 4 times

Search




Leave an answer