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;
}


}
}

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

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).

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.

commented: By my opinion the site of your is nothing special -1
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.

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.

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

If you want to check whether a string is equal to another, use the .equals method. For example:

...
String one = "X";
String two = "X";
if (one.equals(two))
{ System.out.println(one + " = " + two); }
...

The output should be

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.