Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"If" statements not registering as true? Help :(

Status
Not open for further replies.

rpgaction

Technical User
Aug 25, 2006
2
0
0
US
I'm in the process of coding a simple command-line tic-tac-toe game for extra credit in my AP Computer Science I class. I've come across a bit of a hitch...no matter what I enter for playerSymbol when running the program (certain values should exceute one of the "if/else if" blocks), the block of code for my "else" is executed. It compiles fine, however. Could you take a look and see what could be wrong?

Code:
 	//the charSelection method, which lets the player decide to be an X or an O
 	public static void charSelection() throws IOException{
 		
 		/*declares the standard input stream and declares variables 
 		*that determine whether the player is an X or an O*/
 		BufferedReader br = new BufferedReader
 			(new InputStreamReader(System.in));
 		String playerSymbol;
 		char compSymbol;
 		
 		//asks the player to choose X or O
  		System.out.print("\tWould you like X's or O's? Please choose one ");
  		System.out.print("(enter X or O):");
  		playerSymbol = br.readLine();
  		
  		//ensures the player entered X or O
  		if (playerSymbol == "X"){
  			compSymbol = 'O';
  			game(); //calls method game()
  		}
  		
  		else if (playerSymbol == "x"){
  			playerSymbol = "X";
  			compSymbol = 'O';
  			game();
  		}
  		
  		else if (playerSymbol == "O"){	
  			compSymbol = 'X';
  			game();
  		}
  			
  		else if (playerSymbol == "o"){
  			playerSymbol = "O";
  			compSymbol = 'X';
  			game();
  		}
  		
  		else{
  			System.out.print("\tPlease enter either X or O.\n");
  			charSelection();
  		}
  		
  	}
 
Is there any way to edit/delete my post? The layout of this site is confusing. Anyways, I figured out what I was doing wrong (the ==).
 
Once you post, there's no way back.

Glad you notices the old equals thingie :)

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top