| | |
tic-tac-toe!
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Nov 2007
Posts: 1
Reputation:
Solved Threads: 0
ok so im a new programmer and im making a tic tac toe program and i got this error
"operator && cannot be applied to boolean,java.lang.String" in the crazy long line in the winner method and am not sure how to fix it
import java.util.Scanner;
import java.util.Random;
public class tictactoe
{
public static void main(String[] args)
{
Scanner keyboard=new Scanner(System.in);
String[][] board= {{" "," "," "},{" "," "," "},{" "," "," "}};
int x=0;
int y=0;
int option=0;
String player1;
String player2;
System.out.println("Enter 1 for 2 player, or press 7 to exit");
option=keyboard.nextInt();
if (option==1)
{
System.out.println("Player 1 enter your name");
player1=keyboard.next();
System.out.println("Player 2 enter your name");
player2=keyboard.next();
boolean w=winner(board);
while (w==true||w==false)
{
System.out.println(player1+" enter your choice");
x=keyboard.nextInt();
y=keyboard.nextInt();
if (board[x][y]=="X")
{
System.out.println("That space is already taken");
continue;
}
if (board[x][y]=="Y")
{
System.out.println("That space is already taken");
continue;
}
board[x][y]="X";
displayBoard(board);
do
{
System.out.println(player2+" enter your choice");
x=keyboard.nextInt();
y=keyboard.nextInt();
if (board[x][y]=="X")
{
System.out.println("That space is already taken");
continue;
}
if (board[x][y]=="O")
{
System.out.println("That space is already taken");
continue;
}
board[x][y]="O";
displayBoard(board);
break;
}
while (true);
}
}
}
public static void displayBoard (String z[][])
{
System.out.println(" "+z[0][0]+" | "+z[0][1]+" | "+z[0][2]);
System.out.println(" ---------");
System.out.println(" "+z[1][0]+" | "+z[1][1]+" | "+z[1][2]);
System.out.println(" ---------");
System.out.println(" "+z[2][0]+" | "+z[2][1]+" | "+z[2][2]);
}
public static boolean winner (String z[][])
{
if (z[0][0]=="X"&&z[0][1]=="X"&&z[0][2]=="X"||z[1][0]=="X"&&z[1][1]=="X"&&z[1][2]=="X"||z[2][0]=="X"&&z[2][1]=="X"&&z[2][2]=="X"||z[0][0]=="X"&&z[1][0]=="X"&&z[2][0]=="X"||z[0][1]=="X"&&z[1][1]=="X"&&z[2][1]=="X"||z[0][0]=="X"&&z[1][1]=="X"&&z[2][2]=="X"||z[0][2]=="X"&&z[1][1]=="X"&&z[2][0])
{
return true;
}
}
}
"operator && cannot be applied to boolean,java.lang.String" in the crazy long line in the winner method and am not sure how to fix it
import java.util.Scanner;
import java.util.Random;
public class tictactoe
{
public static void main(String[] args)
{
Scanner keyboard=new Scanner(System.in);
String[][] board= {{" "," "," "},{" "," "," "},{" "," "," "}};
int x=0;
int y=0;
int option=0;
String player1;
String player2;
System.out.println("Enter 1 for 2 player, or press 7 to exit");
option=keyboard.nextInt();
if (option==1)
{
System.out.println("Player 1 enter your name");
player1=keyboard.next();
System.out.println("Player 2 enter your name");
player2=keyboard.next();
boolean w=winner(board);
while (w==true||w==false)
{
System.out.println(player1+" enter your choice");
x=keyboard.nextInt();
y=keyboard.nextInt();
if (board[x][y]=="X")
{
System.out.println("That space is already taken");
continue;
}
if (board[x][y]=="Y")
{
System.out.println("That space is already taken");
continue;
}
board[x][y]="X";
displayBoard(board);
do
{
System.out.println(player2+" enter your choice");
x=keyboard.nextInt();
y=keyboard.nextInt();
if (board[x][y]=="X")
{
System.out.println("That space is already taken");
continue;
}
if (board[x][y]=="O")
{
System.out.println("That space is already taken");
continue;
}
board[x][y]="O";
displayBoard(board);
break;
}
while (true);
}
}
}
public static void displayBoard (String z[][])
{
System.out.println(" "+z[0][0]+" | "+z[0][1]+" | "+z[0][2]);
System.out.println(" ---------");
System.out.println(" "+z[1][0]+" | "+z[1][1]+" | "+z[1][2]);
System.out.println(" ---------");
System.out.println(" "+z[2][0]+" | "+z[2][1]+" | "+z[2][2]);
}
public static boolean winner (String z[][])
{
if (z[0][0]=="X"&&z[0][1]=="X"&&z[0][2]=="X"||z[1][0]=="X"&&z[1][1]=="X"&&z[1][2]=="X"||z[2][0]=="X"&&z[2][1]=="X"&&z[2][2]=="X"||z[0][0]=="X"&&z[1][0]=="X"&&z[2][0]=="X"||z[0][1]=="X"&&z[1][1]=="X"&&z[2][1]=="X"||z[0][0]=="X"&&z[1][1]=="X"&&z[2][2]=="X"||z[0][2]=="X"&&z[1][1]=="X"&&z[2][0])
{
return true;
}
}
}
Hi,
You are "==" operator which checks either sides of references points to the same object .
That is checks the memory location not the contents . So use equals() function to check the contents( "X" in your case)of the String object .
Try to check the value("X") using looping statements rather than a cumbersome lengthy if statement
Hope this helps
You are "==" operator which checks either sides of references points to the same object .
That is checks the memory location not the contents . So use equals() function to check the contents( "X" in your case)of the String object .
Try to check the value("X") using looping statements rather than a cumbersome lengthy if statement
Hope this helps
The important thing is not to stop questioning. Curiosity has its own reason for existing.
-Albert Einstein
-Albert Einstein
•
•
•
•
the crazy long line in the winner method
As the error says, you can't perform boolean operations on something that's not a boolean.
You're (it says, and I trust that it's right) comparing Strings with booleans there, which is not allowed.
In no small part that's no doubt because you wrote that entire crazy long line without giving thought to operator precedence rules, causing comparisons to happen that you are not aware of (and likely never intended).
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
•
•
Join Date: Sep 2007
Posts: 27
Reputation:
Solved Threads: 3
Hi, I see some 'common' mistakes newbie programmers often make.
Have a look at this page to help you out:
http://java-assignment.com/hints.php
The page does not tell you how to solve your assignment, but it does tell you how to write cleaner code. (like splitting up your code in small parts, naming conventions like starting a classname with capital letter, etc)
If someone has comments on the page, please let me know.
Have a look at this page to help you out:
http://java-assignment.com/hints.php
The page does not tell you how to solve your assignment, but it does tell you how to write cleaner code. (like splitting up your code in small parts, naming conventions like starting a classname with capital letter, etc)
If someone has comments on the page, please let me know.
Download java source code examples from http://java-assignment.com/
N Puzzle game, Magic squares, Huffman compression techniques, ...
N Puzzle game, Magic squares, Huffman compression techniques, ...
•
•
Join Date: Oct 2007
Posts: 55
Reputation:
Solved Threads: 5
java Syntax (Toggle Plain Text)
public static boolean winner (String z[][]) { if ( ( z[0][0]=="X" && z[0][1]=="X" && z[0][2]=="X" ) || ( z[1][0]=="X" && z[1][1]=="X" && z[1][2]=="X" ) || ( z[2][0]=="X" && z[2][1]=="X" && z[2][2]=="X" ) || ( z[0][0]=="X" && z[1][0]=="X" && z[2][0]=="X" ) || ( z[0][1]=="X" && z[1][1]=="X" && z[2][1]=="X" ) || ( z[0][0]=="X" && z[1][1]=="X" && z[2][2]=="X" ) || ( z[0][2]=="X" && z[1][1]=="X" && z[2][0]=="X" ) ) return true; }
Ok the last term in that crazy long last line of yours didn't have anything to be compared to, "&&z[2][0])" which was wrong! I presumed it's like the others, and fixed it. Also I think that you would want each comparison to be separate, or else you would get the wrong answer a lot of the time. And finally, if you have a crazy long line like that, it might be better to sort it, like I've done above, makes it a lot easier to follow if you ask me.
•
•
Join Date: Jan 2009
Posts: 2
Reputation:
Solved Threads: 1
Hey, im not sure if you have resolved your problem yet, but when you use the == to compare String objects it compares pointers in memory. I know this was mentioned before, but i also want to mention that Strings are immutable and this means they cannot be changed. because of this, in java, when you have a currently made string containing "X" and you make another containing the same string, java makes them point to the same string in memory. this is why your code may work this time, but for future reference use the .equals() method because it compares the actual contents of the strings character by character and you always get the correct result.
•
•
Join Date: Jan 2009
Posts: 1
Reputation:
Solved Threads: 0
If you are trying to compare string variables you have to use the .equals("string") operator.
For example:
String input = "The Purple Bunny";
String character9 = input.substring(8, 9);
if (character9.equals("l"))
System.out.println("Character 9 is an L");
else
System.out.println("Character 9 is not an L");
variable.equals("string") == true is REDUNDANT because the default evaluation for .equals("") is true. To check if it is false, however, simply use variable.equals("string") == false
Hope this helps.
◊The Purple Bunny
For example:
String input = "The Purple Bunny";
String character9 = input.substring(8, 9);
if (character9.equals("l"))
System.out.println("Character 9 is an L");
else
System.out.println("Character 9 is not an L");
variable.equals("string") == true is REDUNDANT because the default evaluation for .equals("") is true. To check if it is false, however, simply use variable.equals("string") == false
Hope this helps.
◊The Purple Bunny
•
•
Join Date: Oct 2009
Posts: 18
Reputation:
Solved Threads: 0
0
#8 Nov 1st, 2009
If you want to check whether a string is equal to another, use the .equals method. For example:
The output should be
Try using it with your strings. I would be quite a long code segment
but it would work if you do it correctly.
Hope it helped.
Java Syntax (Toggle Plain Text)
... String one = "X"; String two = "X"; if (one.equals(two)) { System.out.println(one + " = " + two); } ...
Java Syntax (Toggle Plain Text)
X = X
but it would work if you do it correctly.
Hope it helped.
![]() |
Similar Threads
- Tic-Tac-Toe (C++)
- Tic Tac Toe Homework (C++)
- Tic Tac toe help (C)
- Tic Tac Toe AI help, where to reset variables. (C++)
Other Threads in the Java Forum
- Previous Thread: CodeEditor = problem
- Next Thread: Horizontal and Vertical control on button group
Views: 7713 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for Java
add android api apple applet application arguments array arrays automation binary bluetooth chat chooser class classes client code component converter database detection digit draw eclipse equation error event exception file fractal functiontesting game givemetehcodez graphics gui health helpwithhomework html hyper ide idea image input int integer j2me java javame javaprojects jmf jni jpanel julia linux list loop main map method methods mobile myregfun netbeans newbie nonstatic number object oracle os pattern pearl print problem program programming project recursion scanner screen scrollbar server set size sms socket sort spamblocker sql sqlserver string superclass swing test thread threads time transfer tree windows






