简体   繁体   中英

Printing integer from 2D array using nested FOR Loops

I'm having a bit of a problem with a piece of Java code, part of an AI project. The programm is supposed to take a 11x13 2d array representing a maze. The printed maze we are given uses characters for each cell, but for ease of use I've converted it to integers using a mnemonic code.

My problem is when I try to print the 2d integer array to the screen, to check eveything is OK, I get zeros at every cell, even though I have a check function in place which parses the array cell-by-cell checking for incorrect values.

The project is currently composed of 2 files. The main file - function (AISemesterProject.java) and a file that will implement the UCS algorithm in the future (UCS.java)

AISemesterProject.java

package aisemesterproject;

public class AISemesterProject
{
    public static void main(String[] args)
    {
        UCS a = new UCS();

        a.checkArrayInt();
        a.printInt();
    }
}

UCS.java

package aisemesterproject;

import java.util.Arrays;

public class UCS
{
    int row = 11;
    int col = 13;
    int[][] array_int = new int[row][col];

    public UCS()
    {
        // Lets assume
        // x = 0
        // e = 1
        // d = 2
        // s = 8
        // g = 9
        int[][] array_int = new int[][] {
            {0,1,0,1,1,1,1,0,0,1,0,9,0},
            {1,1,1,2,0,1,1,0,0,1,2,1,0},
            {0,1,0,1,1,1,1,0,0,1,0,0,0},
            {8,1,2,0,1,2,0,1,1,2,1,1,1},
            {0,0,1,1,0,1,1,1,0,0,0,0,0},
            {1,2,1,0,1,0,1,1,0,0,1,1,1},
            {0,1,2,0,1,0,0,2,1,1,2,1,9},
            {1,0,1,1,2,1,1,1,0,1,1,1,1},
            {1,1,2,1,1,0,0,1,0,0,0,0,0},
            {0,0,1,1,1,0,0,1,1,1,1,1,2},
            {0,0,1,0,0,1,1,1,0,9,0,1,1}
        };
    }

    public void checkArrayInt()
    {
        int i = 0, j = 0;
        boolean checker = false;

        for(i = 0; i < row; i++)
        {
            for(j = 0; j < col; j++)
            {
                if(!(array_int[i][i] == 0 || array_int[i][j] == 1 || array_int[i][j] == 2 || array_int[i][j] == 8 || array_int[i][j] == 9))
                {
                    checker = true;
                    System.out.print("Error at Row:" + i + " Column:" + j + "\n");
                }
            }
        }

        if(checker == false)
        {
            System.out.print("Array OK... \n");
        }
    }

    public void printInt()
    {
        int i = 0, j = 0;

        //System.out.println(Arrays.deepToString(array_int));

        for(i = 0; i < row; i++)
        {
            System.out.print("Row " + (i + 1) + ":");

            for(j = 0; j < col; j++)
            {
                System.out.print(" " + String.valueOf(array_int[i][j]));
                //System.out.print(" " + Integer.toString(array_int[i][j]));
                //System.out.printf(" %d", array_int[i][j]);
                //System.out.print(" " + array_int[i][j]);
            }

            System.out.print("\n");
        }
    }
}

Output

输出量

As you can see the output is not what I expected and I have tried 4 different methods for the print (1 active, 3 commented) but the result is always the same.

Anyone have an idea what am I missing or doing wrong?

Thanks for your time.

It looks like you have a scope issue...

int[][] array_int = new int[row][col];

public UCS()
{
    // Lets assume
    // x = 0
    // e = 1
    // d = 2
    // s = 8
    // g = 9
     array_int = new int[][] {
        {0,1,0,1,1,1,1,0,0,1,0,9,0},
        {1,1,1,2,0,1,1,0,0,1,2,1,0},
        {0,1,0,1,1,1,1,0,0,1,0,0,0},
        {8,1,2,0,1,2,0,1,1,2,1,1,1},
        {0,0,1,1,0,1,1,1,0,0,0,0,0},
        {1,2,1,0,1,0,1,1,0,0,1,1,1},
        {0,1,2,0,1,0,0,2,1,1,2,1,9},
        {1,0,1,1,2,1,1,1,0,1,1,1,1},
        {1,1,2,1,1,0,0,1,0,0,0,0,0},
        {0,0,1,1,1,0,0,1,1,1,1,1,2},
        {0,0,1,0,0,1,1,1,0,9,0,1,1}
    };

you already created the array as a class level variable

Your constructor is setting the local variable array_int . This local variable overshadows the field with the same name, and thus it never sees the array you're assigning to it.

You should make sure that you're assigning to the field, which can most easily be done by removing the int[][] word from your constructor.

Thank you everyone. I moved the declatarion from the constructor to the variable at the beggining and it worked.

package aisemesterproject;

import java.util.Arrays;

public class UCS
{
    int row = 11;
    int col = 13;

    // Lets assume
    // x = 0
    // e = 1
    // d = 2
    // s = 8
    // g = 9

    int[][] array_int = new int[][] {
        {0,1,0,1,1,1,1,0,0,1,0,9,0},
        {1,1,1,2,0,1,1,0,0,1,2,1,0},
        {0,1,0,1,1,1,1,0,0,1,0,0,0},
        {8,1,2,0,1,2,0,1,1,2,1,1,1},
        {0,0,1,1,0,1,1,1,0,0,0,0,0},
        {1,2,1,0,1,0,1,1,0,0,1,1,1},
        {0,1,2,0,1,0,0,2,1,1,2,1,9},
        {1,0,1,1,2,1,1,1,0,1,1,1,1},
        {1,1,2,1,1,0,0,1,0,0,0,0,0},
        {0,0,1,1,1,0,0,1,1,1,1,1,2},
        {0,0,1,0,0,1,1,1,0,9,0,1,1}
    };

    public UCS()
    {
        // Lets assume
        // x = 0
        // e = 1
        // d = 2
        // s = 8
        // g = 9

        // Array initialization outside the constructor scope
    }

    public void checkArrayInt()
    {
        int i = 0, j = 0;
        boolean checker = false;

        for(i = 0; i < row; i++)
        {
            for(j = 0; j < col; j++)
            {
                if(array_int[i][j] == 0) //Check for 0 = x
                {
                    checker = false;
                }
                else if(array_int[i][j] == 1) //Check for 1 = e
                {
                    checker = false;
                }
                else if(array_int[i][j] == 2) //Check for 2 = d
                {
                    checker = false;
                }
                else if(array_int[i][j] == 8) //Check for 8 = s
                {
                    checker = false;
                }
                else if(array_int[i][j] == 9) //Check for 9 = g
                {
                    checker = false;
                }
                else //All other integers, which are false
                {
                    checker = true;
                    System.out.print("Error at Row:" + i + " Column:" + j + "\n");
                }
           }
        }

        if(checker == false)
        {
            System.out.print("Array OK... \n");
        }
   }

    public void printInt()
    {
        int i = 0, j = 0;

        //System.out.println(Arrays.deepToString(array_int));

        for(i = 0; i < row; i++)
        {
             System.out.print("Row " + (i + 1) + ":");

            for(j = 0; j < col; j++)
            {
                System.out.print(" " + String.valueOf(array_int[i][j]));
                //System.out.print(" " + Integer.toString(array_int[i][j]));
                //System.out.printf(" %d", array_int[i][j]);
                //System.out.print(" " + array_int[i][j]);
            }

            System.out.print("\n");
        }
    }
}

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM