This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes cannot be applied to a boolean. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "cannot be applied to a boolean." Watch "cannot be applied to a boolean." New topic
Author

cannot be applied to a boolean.

Judith
Greenhorn

Joined: Jan 11, 2005
Posts: 18
Hey all,
I have tried everything i can think of to make this go.
I am trying to hide the mines with this one line.(it's in bold)
I keep getting error messages that state || cannot be applied to a boolean.
any help would be appreciated.
thanks


public class App {
public static void main(String[] args) {
Game game = new Game();
game.play();
}
}

class Game {
private Minefield m = new Minefield();
private Player p = new Player();
public void play() {
m.hideMines();
m.enterGrid(0, 0);
m.drawField();
System.out.println("Game over");
}
}

class Minefield {
private char[] field = new char[4 * 4];
public void hideMines() {
int mines = 0;
do {
int loc = ((int)(Math.random() * 1000)) % (field.length - 1);
if (loc != 0 && field[loc] != 'M') {
field[loc] = 'M';
mines++;
}
} while (mines < 4);
field[15] = 'E';
}
public boolean enterGrid(int pRow, int pCol) {
field[(pRow * 4) + pCol] = 'P';
return false;
}
public void exitGrid(int pRow, int pCol) {
}


public void drawField() {
for (int r = 0; r < 4; r++) {
for (int c = 0; c < 4; c++) {
///THIS LINE RIGHT HERE ---> if (field[(r * 4) + c] ==0________________________________) {

System.out.print("- ");
}
else {
System.out.print(field[(r * 4) + c] + " ");
}
}
System.out.println("");
}
}
}

class Player {
private int row;
private int col;
public void move() {
System.out.print("(r)ight or (d)own: ");
}
public int getRow() {
return row;
}
public int getCol() {
return col;
}
}
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Well, you've removed the part with the || in it, so it's difficult to tell what's wrong. You're comparing a char to an int which should be fine. If not, change it tobut again I don't think that's the problem. Most likely the line actually looks like this:If so, we really need to see the "..." to know what's wrong. As you probably know, || is the logical or operator which requires a boolean expression on both sides. I'm guessing the error you actually got waswhere "int" is anything other than "boolean." No fair hiding part of the error message from us!

Okay, I've tested with a little program and the character from field is promoted to int automatically in JDK 1.5, so the problem must be in the "..." that you aren't showing us. Post that part and the full error if you don't get it yourself. In fact, post it anyway!
Judith
Greenhorn

Joined: Jan 11, 2005
Posts: 18
here is how it reads now.
but it isn't what i wanted completly.
i want to test the field location to see if it's empty OR if contain
a 'mine'.

as it stands now, it shows up.(where the mines are)
here is what you were asking for. i need to change the if comparison.
thanks again..
__________________________________________
public void drawField() {
for (int r = 0; r < 4; r++) {
for (int c = 0; c < 4; c++) {
if (field[(r * 4) + c] == 0) {
System.out.print("- ");
}
else {
System.out.print(field[(r * 4) + c] + " ");
}
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
You initialize the minefield with M's for the hidden locations of the mines, correct? In that case and the empty case (0) you want a dash, correct? Normally, it would be best if you posted what you had originally that didn't work, and then I could provide a hint in the right direction. But in this case I suspect you just had a typo. Here's what you want:Because == and other logical comparisons (<, >, <=, ...) have higher precedence than || and &&, both comparisons get evaluated to boolean values and then or'd together.

While logically that's what happens, in practice if the first test evalutes to true, the || realizes that the second test is unimportant and doesn't perform it. This is called short-circuit logic.

You might also want to declare constants to represent the various values of the minefield. This makes your code more readable.If you're not familiar with the switch statement, don't worry about it for now. You'll get to it later. I'm just demonstrating how constants can make the code easier to read.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
Judith,

I am sorry to see that after 18 posts you still do not have a first and last name separated by a space.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
 
jQuery in Action, 2nd edition
 
subject: cannot be applied to a boolean.
 
Similar Threads
Drawing Problems with JPanel /Jframe need optimize solution.
Block Sliding Puzzle
Could you please help me?
implementing objects in a private class to an array constructor...
synchronization.. and threads !!!