Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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");
    }
  }
}
share|improve this question
    
what is value of x and y? post full code –  Nikolay Kuznetsov Dec 4 '12 at 14:49
3  
you are using = in your if conditions, not == –  jlordo Dec 4 '12 at 14:50

2 Answers 2

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 
share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.