user23054
user23054

Reputation: 21

triple nested loop prints excessive characters

I am working on a program and in I try to print a board that is 10x10. If my object coordinates match the iteration of the i and j loop integers then the object's char should print if not the loop should print '- '. However on my third nested loop since 15 of the objects coordinates dont match the program prints excessive amounts of '- '. How can I simply print the char when one of the coordinates match while keeping the board form. The board should look like this

. . . . . . . . a .
. . e . . . b . . .
. . . . . . . . . .
. . . .c . . . . . .
. . . . . . . d . .
. . g . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . h . . . . .
. . . . . . . . . .

and my code for the print method is

public static void printGrid(bumpercar bcar[], int NUMCARS)
{
    //nested loop
    for(int j = 0; j < 16; j++)
    {
    System.out.printf("\n");
        for(int k = 0; k<16; k++)
        {
            for(int l = 0; l<NUMCARS; l++)
             {
            if((bcar[l].getX() == k) && bcar[l].getY() == j)
            System.out.printf("%s", bcar[l].getCarSymbol());
            else
            System.out.printf("- ");
            }
        }
    }
}

resulting in something like

..........K..................................................................... ......................................... ................................................................................ ................................................ .................................................N.............................. ......................................... ....................I........................................................... ......................................... ..........................F.....................E............................L.. ........................... ........................H...M.P...................O........J.................... ............. .............C...G.............................................................. ..............B............ ..................................A........................................D.... .................................. ................................................................................ ................................................

any ideas how to format the if statements to achive this? thanks

Upvotes: 0

Views: 1042

Answers (2)

Greg Jandl
Greg Jandl

Reputation: 850

The problem is that you're printing a character each time you find a car that doesn't need to be at a given cell.

Fixed code:

public static void printGrid(bumpercar bcar[], int NUMCARS)
{
    //nested loop
    for(int j = 0; j < 16; j++)
    {
        System.out.printf("\n");
        for(int k = 0; k<16; k++)
        {
            int l;
            for(l = 0; l<NUMCARS; l++)
            {
                if((bcar[l].getX() == k) && bcar[l].getY() == j) break;
            }
            if (l == NUMCARS) {
                // no car at this location
                System.out.printf("- ");
            } else {
                System.out.printf("%s", bcar[l].getCarSymbol());
            }
        }
    }
}

Upvotes: 0

Brigham
Brigham

Reputation: 14524

Change your innermost loop to:

        boolean found = false;
        for(int l = 0; l<NUMCARS; l++)
        {
          if((bcar[l].getX() == k) && bcar[l].getY() == j) {
            System.out.printf("%s", bcar[l].getCarSymbol());
            found = true;
            break;
          }
        }
        if (!found) {
            System.out.printf("- ");
        }

Upvotes: 1

Related Questions