I have the following code which allows a user to play Rock, Paper, Scissors with the computer. The code allows for 10 rounds and counts the number of wins and ties. I need to allow for user misspelling by allowing the input to be accepted if the user types the first two letters of the word correctly ie: roxx, paxxx, scxxxxx. I'm not sure how to go about doing this or even where I would write it into my code.

import java.util.Random;
import java.util.Scanner;

public class RockPaperScissors
{
    public static void main(String[] args)
    {
        Scanner in= new Scanner (System.in);
        String yourTurn;
        int computerTurn;
        String computerString;  
        int yourTurnCount = 0;
        int computerTurnCount = 0;
        int tieCount = 0;

        for (int i=1; i<=10; i++)
        {

        System.out.print ("Enter your play: rock, paper or scissors");
        yourTurn = in.nextLine();
        Random generator = new Random();
        computerTurn = generator.nextInt (3) +1;
        
        if (computerTurn == 1)
            computerString = "rock";
        else if (computerTurn == 2 )
            computerString = "paper";
        else
            computerString = "scissors";

        System.out.println ("Your pick: " + yourTurn);
        System.out.println ("Computer pick:" + computerString);
        
        if (yourTurn.equalsIgnoreCase(computerString))
        {
            System.out.println ("It's a tie!");
        	tieCount++;
        }
        else if (yourTurn.equalsIgnoreCase("rock") && computerString.equalsIgnoreCase("paper"))
        {
            System.out.println("Computer wins!");
        	computerTurnCount++;
        }
        else if (yourTurn.equalsIgnoreCase("rock") && computerString.equalsIgnoreCase("scissors"))
        {
            System.out.println("You win!");
            yourTurnCount++;
        }
        else if (yourTurn.equalsIgnoreCase("paper") && computerString.equalsIgnoreCase("rock"))
        {
            System.out.println("You win!");
            yourTurnCount++;
        }
        else if (yourTurn.equalsIgnoreCase("paper") && computerString.equalsIgnoreCase("scissors"))
        {
            System.out.println("Computer wins!");
        	computerTurnCount++;
        }
        else if (yourTurn.equalsIgnoreCase("scissors") && computerString.equalsIgnoreCase("rock"))
        {
            System.out.println("Computer wins!");
        	computerTurnCount++;
        }
        else if (yourTurn.equalsIgnoreCase("scissors") && computerString.equalsIgnoreCase("paper"))
        {
            System.out.println("You win!");
            yourTurnCount++;
        }
        
        }

        System.out.println("Your wins: " + yourTurnCount + "\nComputer wins: " + computerTurnCount + "\nTies: "
        		+ tieCount);

}
}

use the startsWith("ro") method for rock, instead of equals("rock").
in order to make up for the IgnoreCase,

if (input.toLowerCase()).startsWith("ro")...

I have done the following and for the most part it works well but when I type in SClkdjaflk it does not work and should count that as scissors. Any adivice?

import java.util.Random;
import java.util.Scanner;

public class RockPaperScissors
{
    public static void main(String[] args)
    {
        Scanner in= new Scanner (System.in);
        String yourTurn;
        int computerTurn;
        String computerString;  
        int yourTurnCount = 0;
        int computerTurnCount = 0;
        int tieCount = 0;

        
        for (int i=1; i<=10; i++)
        {

        System.out.print ("Enter your play: rock, paper or scissors");
        yourTurn = in.nextLine();
        
        Random generator = new Random();
        computerTurn = generator.nextInt (3) +1;
        
        
        if (computerTurn == 1)
            computerString = "rock";
        else if (computerTurn == 2 )
            computerString = "paper";
        else
            computerString = "scissors";

        System.out.println ("Your pick: " + yourTurn);
        System.out.println ("Computer pick:" + computerString);
        
        
        if (yourTurn.equalsIgnoreCase(computerString))
        {
            System.out.println ("It's a tie!");
        	tieCount++;
        }
        else if (yourTurn.toLowerCase().startsWith("ro") && computerString.equalsIgnoreCase("paper"))
        {
            System.out.println("Computer wins!");
        	computerTurnCount++;
        }
        else if (yourTurn.toLowerCase().startsWith("ro") && computerString.equalsIgnoreCase("scissors"))
        {
            System.out.println("You win!");
            yourTurnCount++;
        }
        else if (yourTurn.toLowerCase().startsWith("pa") && computerString.equalsIgnoreCase("rock"))
        {
            System.out.println("You win!");
            yourTurnCount++;
        }
        else if (yourTurn.toLowerCase().startsWith("pa") && computerString.equalsIgnoreCase("scissors"))
        {
            System.out.println("Computer wins!");
        	computerTurnCount++;
        }
        else if (yourTurn.toLowerCase().startsWith("sc") && computerString.equalsIgnoreCase("rock"))
        {
            System.out.println("Computer wins!");
        	computerTurnCount++;
        }
        else if (yourTurn.toLowerCase().startsWith("sc") && computerString.equalsIgnoreCase("paper"))
        {
            System.out.println("You win!");
            yourTurnCount++;
        }
        else
        	System.out.println("Invalid Input...Please Try Again!");
        
        }



        System.out.println("Your wins: " + yourTurnCount + "\nComputer wins: " + computerTurnCount + "\nTies: "
        		+ tieCount);

}
}

Try giving SCDGHFHFDHD as the input. If it works fine, then there's a problem when SCdgfgd is converted to lowercase. If there's a problem in that, then you should -

A. Either trim the String and check with .equalsIgnoreCase()
B. Convert it selectively after performing .toCharArray() on it.

I'd suggest "A" since "B" would be too long. Just trim the String or input to accept only the first two characters and check them with .equalsIgnoreCase(). :)

you can't accept only the two first char, since you have to print out the original first.

try extracting the first two characters with the substring method of String, set it to lowercase, and then you can perform equalsIgnoreCase

you can't accept only the two first char, since you have to print out the original first.

try extracting the first two characters with the substring method of String, set it to lowercase, and then you can perform equalsIgnoreCase

That's what I meant, by trimming the first two chars.

Isn't it possible, though? If you're using a BufferedReader to read the String char-by-char, you can stop at two chars by limiting the loop, can't you? You don't need to use readLine() for that!

That's what I meant, by trimming the first two chars.

Isn't it possible, though? If you're using a BufferedReader to read the String char-by-char, you can stop at two chars by limiting the loop, can't you? You don't need to use readLine() for that!

sure that is possible: but since he first needs to print something like:

you entered this: <inputted text>

he can not use that, he'll need to get the complete text. also, trimming does not do what you describe it to do. it removes leading and trailing spaces of a String, not just take a substring.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.