1

I've been trying to fix this program for a while. Basically, it's a Rock-Paper-Scissors type of game, and everything works apart from the input validation. Any help would be appreciated.

This is my code:

import java.util.Scanner;
public class RockPaperScissors
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner (System.in);
        System.out.println("Player 1: Choose rock, paper or scissors:");
        String player1 = scan.next() .toLowerCase();
        //Player 1 Input Validation
        if ((player1 != ("rock"))
        || (player1 != ("paper"))
        || (player1 != ("scissors")))
        {
            System.out.println("Thats not right, choose rock, paper or scissors");
        }
        //Send Back to input
        System.out.println("Player 2: Choose rock, paper or scissors:");
        String player2 = scan.next() .toLowerCase();
        //Player 2 Input Validation
        if ((player2 != ("rock"))
        || (player2 != ("paper"))
        || (player2 != ("scissors")))
        {
            System.out.println("Please choose rock, paper or scissors");
        }
        System.out.println("Player 1 chose " + player1);
        System.out.println("Player 2 chose " + player2);


    //For Player 1 to win
    if((player1.equals("rock"))&&(player2.equals("scissors"))
    ||(player1.equals("scissors"))&&(player2.equals("paper"))
    ||(player1.equals("paper"))&&(player2.equals("rock")))
    {
        System.out.println ("Player 1 Wins!");
    }

    //For a draw
    if (player1.equals(player2))
    {
        System.out.println ("Its a Draw!");
    }

    //For Player 2 to win
    if ((player2.equals("rock"))&&(player1.equals("scissors"))
    ||(player2.equals("scissors"))&&(player1.equals("paper"))
    ||(player2.equals("paper"))&&(player1.equals("rock")))
    {
        System.out.println("Player 2 wins!");
    }

}

And this is the output that I get (Rock and Paper, are user inputs):

Player 1: Choose rock, paper or scissors:  
Rock  
Thats not right, choose rock, paper or scissors  
Player 2: Choose rock, paper or scissors:  
Paper   
Please choose rock, paper or scissors  
Player 1 chose rock  
Player 2 chose paper  
Player 2 wins!  
Chris Forrence
  • 10,042
  • 11
  • 48
  • 64

2 Answers2

3

instead of using == and != use the equals() and equalsIgnoreCase() methods when you want to compare strings.

and when using a Scanner use the nextLine() method instead of next().

Use constants instead of variable strings.

and when the player's input is not right and you want the player to give the input again, its best to use a looping structure like while and when the user has given a valid input then break out of the loop or use the loop inside a function and return a value.

like

Edit: to answer your question on the comments try this

class Player {
  String choice;
}

class Game {

static final ROCK = "ROCK";
static final PAPER = "PAPER";
static final SCISSORS = "SCISSORS";

  Scanner scanner = new Scanner();
  Player[] players = new Player[2]; //assuming you only want 2 players Use ArrayList for unspecified number of users and use a separate method to initialize it.

 public Game {
      for(int i =0 ; i<2 ; i++) {
         players[i]= new Player();
      }
 }

 private String getPlayerChoice() {
    System.out.println("Please enter your choice");
    System.out.println("1.Rock\n2.Paper\n3.Scissors")
    return scanner.nextLine();
 }

 void getInputAndValidate(Player p) {
  p.choice = getPlayerChoice();
  while(true) {
    if(p.choice.equalsIgnorecase(ROCK) || 
       p.choice.equalsIgnorecase(PAPER) || 
       p.choice.equalsIgnorecase(SCISSORS) ) {
         break;
    }
    else {
        System.out.println("Please enter a valid input");
        p.choice = getPlayerChoice(); \\ use the scanner as an instance variable.
    }
  }  
 }

 void getInput() {
    for(int i = 0; i<2;i++) {
     System.out.println("Player " + (i+1));
     getInputAndValidate(players[i]);
    }
 }

void compute(){
  // implement your game logic here
}
public static void main(String[] args) {
  Game g = new Game();
  g.getInput();
  g.compute();
}

}
Thirumalai Parthasarathi
  • 4,541
  • 1
  • 25
  • 43
  • I used this, and i'm getting an infinite loop when i run? is it because i only used the while part? – Kathryn-May Forrest Nov 04 '13 at 15:22
  • if you do not use a break statement `while(true)` always remains true and the loop will go into infinite execution. The break statement breaks the control flow out of the loop when the inputed value is a valid one. – Thirumalai Parthasarathi Nov 04 '13 at 15:26
  • that should come after the while loop... the entire while loop is used only for validation... as i told u put this in a method and call the method by passing the input of the firstplayer then prompt the second player to give the input. call the method again with the user iunput to validate. then implement the game logic. – Thirumalai Parthasarathi Nov 04 '13 at 15:34
  • @Kathryn-MayForrest: glad i was of help, i've edited the post to give you a clearer answer and implementation. – Thirumalai Parthasarathi Nov 04 '13 at 16:04
1
if ((player1 != ("rock"))
    || (player1 != ("paper"))
    || (player1 != ("scissors")))

Change all your != to equals or equalsIgnoreCase() to avoid case mix ups

if ((!player1.equalsIgnoreCase("rock"))
    || (!player1.equalIgnoreCase("paper"))
    || (!player1.equalsIgnoreCase("scissors")))
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720