0

This relates to the method userInput() and its if statements. The output for userInput() is 0 no matter what, why? I know the initial value of calculate = 0; but i want to change it so that each time the user enters "Paper" for example the value of calculate = 1; IVE TRIED EVERYTHING! now im stuck wondering whats wrong with my if statements. Please help. (sorry if i'm a noob)

import java.util.Scanner;

class RPS{

    private static String[] userOption = new String[]{"Rock", "Paper","Scissors"};
public static void main(String[] args){

    System.out.println("Enter: Rock, Paper Or Scissors");
    System.out.println(userInput());        
}

    public static int userInput(){

        int calculate = 99;

         Scanner input = new Scanner(System.in);
         String userChoice = input.nextLine();

            if(userChoice == userOption[0]){
                calculate = 0;
            } else if(userChoice == userOption[1]){
                calculate = 1;
            } else if(userChoice == userOption[2]){
                calculate = 2;
            }           

            return calculate;
        }   
}
Roman C
  • 49,761
  • 33
  • 66
  • 176
BriannaXD
  • 169
  • 2
  • 14

1 Answers1

0

You have to use the equals Method if you are working with strings.

e.g.:

if(userChoice.equals(userOption[0]))

Because if you use == it checks whether the references to the objects are equal.

Ben
  • 1,157
  • 6
  • 11
  • No problem, glad to help. ;) If this solution helped you, accept this as an answer to mark your question as answered. – Ben Dec 14 '13 at 10:45
  • Says ive have to do it in 2 mins. But i will make sure i do! Thanks again :D – BriannaXD Dec 14 '13 at 10:47