Printing boolean values from an array
I am having difficulty with a particular segment of a project and have created a test class in an attempt to debug it. The project requires a multi-dimensional array consisting of boolean values which I set to false and then specific coordinates of that grid to true. I then have a method with an if statement that if that boolean value is true then do such and such. It works backwards. Trying to look into the cause of this I created the following class:
Code java:
public class booleanArrayTest {
/**
* @param args
*/
public int n = 9;
public boolean[][] start = new boolean[n][n];
public void falseStart() {
for (int x = 0; x < n; x++) {
for (int y = 0; y < n; y++) {
start[x][y] = false;
}
}
}
public void displayStart() {
for (int x = 0; x < n; x++) {
for (int y = 0; y < n; y++) {
if (start[x][y] = false) {
System.out.println(start[x][y]);
}
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
booleanArrayTest ArrayTest = new booleanArrayTest();
ArrayTest.falseStart();
ArrayTest.displayStart();
}
}
Why can't I print those boolean values from the grid in this manner?
Thanks in advance
Re: Printing boolean values from an array
For future reference, please flank your code with the code tags. In addition, it helps to post any and all error messages you are getting. In this case, I presume its a type mismatch on the line
if ( start[x][y] = false )
which should be a boolean
if ( start[x][y] == false)
Re: Printing boolean values from an array
Thanks, I appreciate it. Now my program works fine, I had a feeling I was overlooking something obvious. Also, I actually was not getting an error message, I was getting the opposite of the results I was expecting when I ran my program.
Thanks again,
Deprogrammer
Re: Printing boolean values from an array
Quote:
I actually was not getting an error message, I was getting the opposite of the results I was expecting when I ran my program.
This code
Code :
if ( start[x][y] = false )
assigns the boolean value false to start[x][y] and tests that value, which is (oh so obviously) false, and the if block is never entered.
db