This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes 2D Boolean array and randomness Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "2D Boolean array and randomness " Watch "2D Boolean array and randomness " New topic
Author

2D Boolean array and randomness

D diller
Greenhorn

Joined: Nov 15, 2007
Posts: 29
Below is some simple code I am working on to essentially randomly create true or false values in a 2D boolean array. Presently I am using the following code, and it keeps on making the first row (board[0][x]) 'true' and all other values false, which makes me wonder:

1 - Are elements in a boolean array automatically assigned a value?
2 - Will my embedded 'while' loops fail after the first row?
3 - Is there an easier way other than making a random number and testing its value against the mid-ground?



Any help would be appreciated.>
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18754
    
  40



The condition is actually not a condition, but an assignment -- notice the single equals sign.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
D diller
Greenhorn

Joined: Nov 15, 2007
Posts: 29
D'oh! Stupid me for overlooking that; all is great now, except that it still only changes around my first row, it's like it doesn't ever actually run the outside-most loop again.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18754
    
  40

D diller wrote:D'oh! Stupid me for overlooking that; all is great now, except that it still only changes around my first row, it's like it doesn't ever actually run the outside-most loop again.


Actually, it doesn't "run" the inner loop again -- notice that you never reset your y index for the next row. It is only set once, hence, it only does one row.

Henry
D diller
Greenhorn

Joined: Nov 15, 2007
Posts: 29
Got it, just had to use a for loop instead on the inside.. Bingo! Thanks again for helping me identify that = instead of ==.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38449
    
  23
D diller wrote:if (board[x][y] = true)
Henry has already commented, but the correct format would be

if (board[x][y]) . . .
D diller
Greenhorn

Joined: Nov 15, 2007
Posts: 29
Campbell Ritchie wrote:
D diller wrote:if (board[x][y] = true)
Henry has already commented, but the correct format would be

if (board[x][y]) . . .

Erm, shouldn't I haveinstead of? Won't it error out on the second way?
D diller
Greenhorn

Joined: Nov 15, 2007
Posts: 29
I'm now having some problems with getting my GUI to display properly - I've looked over all the code I can think of but I just can't narrow down what my problem is.

And the code for the GUI in the only spot I change the color of any cell:
It calls on life.isAlive:
So, simply, it returns true if it's true or false if it's false. No if statements or anything to return; even if I change this to return false; I get the same result.
Does this code look okay for going through and setting a specific number of true values? I tested it on a 3x3 array with numtolive = 2, and it seemed to work fine; my GUI displays all as 'true' though, even if I know 100% for sure the value is false (as in I set everything to false, and even if I didn't the default boolean value is false)

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38449
    
  23
D diller wrote:
Erm, shouldn't I haveinstead of? Won't it error out on the second way?
No. The == false is redundant, and you have already seen, it is too easy to write = instead of == which can cause nasty errors later on.

If you want to test false, you use the "not" operator . . .

if (!board[1][2]) . . .

or

if (!(x < 3 && y > 4)) . . .

Note the additional () because ! has a higher precedence than && or < or >.

So you should never write == true or == false.

I think the second version will throw a compiler error because the == false is outside the ().
 
Don't get me started about those stupid light bulbs.
 
subject: 2D Boolean array and randomness