0

I'm having a problem with my 2 dimensional boolean array. (or it may be with the logic of printing out the values). I set all of the values in the array to false in the beginning, and then later i print out the values to the screen. When i print them out, they all come up as true.

x=20;
y=10;
boolArray = new boolean[x][y];

for(int c=0;c<x;c++)
{
  for(int i=0;i<y;i++)
  {
    boolArray[c][i] = false;
  }
}

System.out.println("2D Boolean Array:");

for(int a = 0; a < boolArray.length; a++)
{
  for(int b = 0; b < boolArray[a].length; b++)
  {
    if(boolArray[a][b] = true)
    {
      System.out.print("T");
    }
    else if(boolArray[a][b] = false)
    {
      System.out.print("F");
    }
  }
}
2
  • what is value of x and y? post full code Commented Dec 4, 2012 at 14:49
  • 3
    you are using = in your if conditions, not == Commented Dec 4, 2012 at 14:50

2 Answers 2

1

This is bad:

if(boolArray[a][b] = true)
    {
      System.out.print("T");
    }
    else if(boolArray[a][b] = false)
    {
      System.out.print("F");
    }

you are using the assignment operator = instead of the comparison operator ==

You could change it to

if(boolArray[a][b] == true)
//...
else if(boolArray[a][b] == false)

or nicer

if(boolArray[a][b])
//...
else if(!boolArray[a][b])

or even nicer:

if(boolArray[a][b])
//...
else 
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

    if(boolArray[a][b])
    {
      System.out.print("T");
    }
    else
    {
      System.out.print("F");
    }

With booleans you can do like

if(boolean field)

You have used = assigment instead of == comparison.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.