0

I am an extreme coding noob and I've just started. I designed a rock paper scissors game and can't figure out why it won't work. For some reason the user input isn't working in the if statement. Please take a look.

    package com.youtube.njillatactics;

    import javax.swing.JOptionPane;

    public class RPS {
public static void main(String args[]){
    //start message
    JOptionPane.showMessageDialog(null, "Welcome to Nick's rock paper scissors game!");
    //get user input and convert to lower case
    String userInput = JOptionPane.showInputDialog("Choose rock, paper, or scissors.").toLowerCase();
    //generate random computer input
    double computerInput = Math.random();
    //match user input to converted computer input
    JOptionPane.showMessageDialog(null, match(userInput, convert(computerInput)));
}
//convert random computer input into choice
public static String convert(double x){
    if(x < 0.33){
        return "rock";
    }else if(x < 0.66){
        return "paper";
    }else
        return "scissors";
}
//check to see who wins
public static String match(String x,String y){
    if(x == y){
        return "Tie!";
    }else
    if(x == "rock"){
        if(y == "paper"){
            return "Computer wins!";
        }else
            return "User wins!";
    } 
    if(x == "paper"){
        if(y == "scissors"){
            return "Computer wins!";
        }else
            return "User wins!";
    }
    if(x == "scissors"){
        if(y == "rock"){
            return "Computer wins!";
        }else
            return "User wins!";
    }else
        return x + ", " + y;
}
}
aliteralmind
  • 19,847
  • 17
  • 77
  • 108
  • Wow. It was that simple. Like I said I'm new so thanks for helping me out. That was quick! – user3461740 Mar 28 '14 at 01:20
  • 1
    For future reference, it would be helpful if you took the time to identify _what_ specifically wasn't working. You didn't tell us the symptoms, much less a specific snippet of code that did one thing when you thought it would do another. – yshavit Mar 28 '14 at 01:21

2 Answers2

5

You are comparing strings with == when you should be comparing them with .equals(s)

Instead of, for example,

if(y == "rock")  {

change it to

if(y.equals("rock"))

(You should also ensure that y is not null either by testing it, or try-catching for it.)

Comparing strings with == evaluates to true if the same string object is on both sides of the ==.

Comparing strings with equals(s) compares their values, regardless if they're the same object...and obviously, if they happen to be the same object, it will always evaluate to true.

aliteralmind
  • 19,847
  • 17
  • 77
  • 108
  • 2
    Another variation would be `if ("rock".equals(y))` – takendarkk Mar 28 '14 at 01:20
  • 1
    "[Yoda conditions](http://blog.codinghorror.com/new-programming-jargon)." – yshavit Mar 28 '14 at 01:23
  • 2
    Even though I fully support your answer, the user might not always put exactly "rock", so you could also do y.equalsIgnoreCase("rock"); and it would make it more universal for the person playing the game – user2277872 Mar 28 '14 at 01:24
  • @yshavit: You think it should be the other way around, as a matter of style. I would (not so strongly) argue that `y.equals("rock")` is preferable from a "fail fast" point of view. Obviously needs to be `try`ed. – aliteralmind Mar 28 '14 at 01:31
  • @user2277872: I'll leave it as is, but it's a good point. – aliteralmind Mar 28 '14 at 01:36
  • 1
    @aliteralmind I wasn't trying to argue one way or another. I just like that name for the pattern. :) – yshavit Mar 28 '14 at 01:38
  • 1
    I wasn't saying you SHOULD use it, I was just giving a suggestion on another approach that might have been more of a user friendly input is all :) – user2277872 Mar 28 '14 at 12:09
  • @user3461740: If you feel this has helped you, please consider up-voting it, and accepting it by clicking on the big green checkmark. Thank you, and best of luck! – aliteralmind Mar 29 '14 at 11:12
1

In java, you do not compare two strings by x == y. Instead, you compare by using x.equals(y). This explains why those if statements do not work.

Lawrence Choy
  • 6,088
  • 1
  • 20
  • 30