944,111 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 18757
  • Java RSS
Nov 2nd, 2007
0

tic-tac-toe!

Expand Post »
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;
}

}
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
en1gmat1c is offline Offline
1 posts
since Nov 2007
Nov 3rd, 2007
0

Re: tic-tac-toe!

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
Reputation Points: 10
Solved Threads: 6
Junior Poster in Training
parthiban is offline Offline
80 posts
since Sep 2006
Nov 3rd, 2007
0

Re: tic-tac-toe!

Quote ...
the crazy long line in the winner method
Start by splitting that line into fragments you can understand.
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).
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Nov 3rd, 2007
-1

Re: tic-tac-toe!

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.
Reputation Points: 30
Solved Threads: 3
Light Poster
schoolsoluction is offline Offline
27 posts
since Sep 2007
Nov 5th, 2007
0

Re: tic-tac-toe!

java Syntax (Toggle Plain Text)
  1. public static boolean winner (String z[][])
  2. {
  3. if ( ( z[0][0]=="X" && z[0][1]=="X" && z[0][2]=="X" )
  4. || ( z[1][0]=="X" && z[1][1]=="X" && z[1][2]=="X" )
  5. || ( z[2][0]=="X" && z[2][1]=="X" && z[2][2]=="X" )
  6. || ( z[0][0]=="X" && z[1][0]=="X" && z[2][0]=="X" )
  7. || ( z[0][1]=="X" && z[1][1]=="X" && z[2][1]=="X" )
  8. || ( z[0][0]=="X" && z[1][1]=="X" && z[2][2]=="X" )
  9. || ( z[0][2]=="X" && z[1][1]=="X" && z[2][0]=="X" ) )
  10. return true;
  11. }

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.
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
mickinator is offline Offline
55 posts
since Oct 2007
Jan 16th, 2009
0

Re: tic-tac-toe!

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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
orienthis is offline Offline
2 posts
since Jan 2009
Jan 24th, 2009
0

Re: tic-tac-toe!

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
◊ThePurpleBunny is offline Offline
1 posts
since Jan 2009
Nov 1st, 2009
0
Re: tic-tac-toe!
If you want to check whether a string is equal to another, use the .equals method. For example:
Java Syntax (Toggle Plain Text)
  1. ...
  2. String one = "X";
  3. String two = "X";
  4. if (one.equals(two))
  5. { System.out.println(one + " = " + two); }
  6. ...
The output should be
Java Syntax (Toggle Plain Text)
  1. X = X
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
theStruggler is offline Offline
18 posts
since Oct 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: CodeEditor = problem
Next Thread in Java Forum Timeline: Horizontal and Vertical control on button group





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC