Computer Science Canada

If statement will not execute.

Author:  Srlancelot39 [ Fri Sep 21, 2012 12:44 am ]
Post subject:  If statement will not execute.

I have tried various combinations of things, and I still cannot find out why the following If statement is not executing.

code:
if(personPlayUpper == "R" && computerPlay == "P")
                {
                        System.out.println("Computer WINS!  Paper covers Rock.");
                }
                else if(personPlayUpper == "P" && computerPlay == "S")
                {
                        System.out.println("Computer WINS!  Scissors cuts Paper.");
                }
                else if(personPlayUpper == "S" && computerPlay == "R")
                {
                        System.out.println("Computer WINS!  Rock breaks Scissors.");
                }
                else if(personPlayUpper == "R" && computerPlay == "S")
                {
                        System.out.println("You WIN!  Rock breaks Scissors.");
                }
                else if(personPlayUpper == "P" && computerPlay == "R")
                {
                        System.out.println("You WIN!  Paper covers Rock.");
                }
                else if(personPlayUpper == "S" && computerPlay == "P")
                {
                        System.out.println("You WIN!  Scissors cuts Paper.");
                }
                else if(personPlayUpper == computerPlay)
                {
                        System.out.println("Tie game.");
                }


The entire code is below:
code:
void q5()
       
        {
                System.out.println("Please enter R(ock), S(cissor) or P(aper)");               
                Scanner scan = new Scanner(System.in);   
                Random generator = new Random();
               
                String personPlay;
               
                String personPlayUpper;
               
                String computerPlay = "N";
               
                int computerInt = generator.nextInt(3); //Randomly generate number used to determine computer's play
               
                personPlay = scan.nextLine(); //Get player's play -- note that this is stored as a string
                personPlayUpper = personPlay.toUpperCase(); //Make player's play uppercase for ease of comparison
               
                if(computerInt == 0) //Translate computer's randomly generated play to string
                {
                        computerPlay = "R";
                        System.out.println("Computer played: Rock");
                }
                else if(computerInt == 1)
                {
                        computerPlay = "P";
                        System.out.println("Computer played: Paper");
                }
                else if(computerInt == 2)
                {
                        computerPlay = "S";
                        System.out.println("Computer played: Scissors");
                }
               
                System.out.println(personPlayUpper);
                System.out.println(computerPlay);
               
                if(personPlayUpper == "R" && computerPlay == "P")
                {
                        System.out.println("Computer WINS!  Paper covers Rock.");
                }
                else if(personPlayUpper == "P" && computerPlay == "S")
                {
                        System.out.println("Computer WINS!  Scissors cuts Paper.");
                }
                else if(personPlayUpper == "S" && computerPlay == "R")
                {
                        System.out.println("Computer WINS!  Rock breaks Scissors.");
                }
                else if(personPlayUpper == "R" && computerPlay == "S")
                {
                        System.out.println("You WIN!  Rock breaks Scissors.");
                }
                else if(personPlayUpper == "P" && computerPlay == "R")
                {
                        System.out.println("You WIN!  Paper covers Rock.");
                }
                else if(personPlayUpper == "S" && computerPlay == "P")
                {
                        System.out.println("You WIN!  Scissors cuts Paper.");
                }
                else if(personPlayUpper == computerPlay)
                {
                        System.out.println("Tie game.");
                }
        }


The two System.out.println commands just above the If statement execute properly, but for some reason, the If statements themselves will not.
Any help is greatly appreciated! Thanks![/code]

Author:  Insectoid [ Fri Sep 21, 2012 7:12 am ]
Post subject:  RE:If statement will not execute.

You're comparing strings. '=' will not work. Use String.equals() instead.

Author:  Srlancelot39 [ Fri Sep 21, 2012 8:06 am ]
Post subject:  RE:If statement will not execute.

Sounds promising. Thanks, I'll try that!

EDIT: Yup, it worked! Thanks again!


: