0

As you might be able to see, i am a complete newbie to java. but i'm trying to make a rock-paper-scissor game. But when i pass the input to the method, it doesn't return true, while the input is "rock". i checked.

What should happen, is that as the input is rock, true gets returned. now it just returns false.

thanks in advance!

import java.util.Scanner;

public class Game{

    static Boolean validInput(String input){
        if("rock" == input){
            return true;
        }
        else{
            return false;
        }
    }

    public static void main(String[] args){
        Scanner input = new Scanner(System.in);

        String choice;
        choice = input.nextLine();

        if(validInput(choice)){
            System.out.println(choice + " is valid input");
        }else{
            System.out.println(choice + " is not valid...");
        }

    }

}
syter
  • 135
  • 2
  • 10

1 Answers1

2

In java, if you want to compare strings you must use .equals() instead of == So you would have

input.equals("rock")

This article will give you more in depth explanation :)
https://www.geeksforgeeks.org/difference-equals-method-java/

This post might be helpful also How do I compare strings in Java?

rhowell
  • 1,165
  • 8
  • 21