Bhagyesh
Bhagyesh

Reputation: 700

How to compare a String with an integer?

How can I compare a string with an int? I am making a Rock-paper-scissors game and how do I turn the string the user enters in to a int so the program can check who had won? Such as if the users enters "rock" the program registers that as 0 and so on?

package rpc;

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

public class Main {
    public static void main(String[] args) {
        /* Random number generator */
        Random random = new Random();

        /* Scanner object for input */
        Scanner scanner = new Scanner(System.in);

        /*
         * Integer variables to hold the user and computer choice.
         * 0 = Rock
         * 1 = Paper
         * 2 = Scissors
         */
        String userChoice;
        int computerChoice;

        // Showing prompt and user input
        System.out.println("Enter move (0 = Rock; 1 = Paper; 2 = Scissors):");
        userChoice = scanner.nextLine();

        // Checking if userChoice is 0, 1, or 2.
        if (!userChoice.equalsIgnoreCase("Scissors") && !userChoice.equalsIgnoreCase("Paper")
            && !userChoice.equalsIgnoreCase("rock")) {
            System.out.println("Invalid choice. Ending program.");

            // Exit program
            Main.main(args);
        }

        // Generating random computer choice
        computerChoice = random.nextInt(3);

        // Determining the winner

        // If the choices are equal, it's a tie.
        if (userChoice == computerChoice) {
            if (userChoice == 0) {
                System.out.println("Both players chose rock!");
            } else if (userChoice == 1) {
                System.out.println("Both players chose paper!");
            } else {
                System.out.println("Both players chose scissors!");
            }

            // Exit program
            System.exit(0);
        }

        if (userChoice == 0) {        // User chooses rock
            if (computerChoice == 1) {
                System.out.println("You chose rock; Computer chose paper");
                System.out.println("Computer wins!");
            } else {
                System.out.println("You chose rock; Computer chose scissors");
                System.out.println("You win!");
            }
        } else if (userChoice == 1) {    // User chooses paper
            if (computerChoice == 0) {
                System.out.println("You chose paper; Computer chose rock");
                System.out.println("You win!");
            } else {
                System.out.println("You chose paper; Computer chose scissors");
                System.out.println("Computer wins!");
            }
        } else {    // User chooses scissors
            if (computerChoice == 0) {
                System.out.println("You chose scissors; Computer chose rock");
                System.out.println("Computer wins!");
            } else {
                System.out.println("You chose scissors; Computer chose paper");
                System.out.println("You win!");
            }
        }
        scanner.close();
    }
}

Upvotes: 0

Views: 456

Answers (3)

Fundhor
Fundhor

Reputation: 3587

Retrieving a random item from ArrayList

This is not the exact answer to your question (Integer.parseInt(myInt)) but you could try something more readable like this, avoiding the use of unnecessary Integers. And simplifies your code

Generate your arrayList and then pick the random "computer" choice.

List<String> posibilities = Arrays.asList("rock","paper","scissors");
String computerChoice = possibilites.get(Math.random(3));

then do your comparaison ;)

/* Chose the possibilities */
List<String> posibilities = Arrays.asList("rock","paper","scissors");

/* Scanner object for input */
Scanner scanner = new Scanner(System.in);

// Showing prompt and user input
System.out.println("Enter move (0 = Rock; 1 = Paper; 2 = Scissors):");
String userChoice = scanner.nextLine();
userChoice = possibilities.get(Integer.parseInt(userChoice));

// Checking if userChoice is 0, 1, or 2.
if(!possibilities.contains(userChoice)) {
    System.out.println("Invalid choice. Ending program.");

    // Exit program
    Main.main(args);
}

// Generating random computer choice
String computerChoice = possibilites.get(Math.random(3));

// Determining the winner

// If the choices are equal, it's a tie.
if(userChoice.equals(computerChoice)) {
    System.out.println("Both players chose " + userChoice);

    // Exit program
    System.exit(0);
}

System.out.println("You chose " + userChoice + "; Computer chose " + computerChoice);
if(userChoice.equals("rock")) {    // User chooses rock
    if(computerChoice.equals("paper")) {
            System.out.println("Computer wins!");
    } else {
        System.out.println("You win!");
    }
}
else if(userChoice.equals("paper")) {  // User chooses paper
    if(computerChoice.equals("rock")) {
        System.out.println("You win!");
    } else {
        System.out.println("Computer wins!");
    }
} else {   // User chooses scissors
    if(computerChoice.equals("Scissors")) {
        System.out.println("Computer wins!");
    } else {
        System.out.println("You win!");
    }
}

scanner.close();

Upvotes: 0

ataulm
ataulm

Reputation: 15334

You could use an enum to enumerate the three possible choices:

enum Hand {

    ROCK,
    PAPER,
    SCISSORS;

    public static Hand from(String input) {
        for (Hand hand : values()) {
            if (hand.name().equalsIgnoreCase(input)) {
                return hand;
            }
        }
        throw new IllegalArgumentException("Invalid choice: " + input);
    }

}

Enums have an intrinsic integer value (that corresponds to the position they were defined at). ROCK.ordinal() will return 0, for example.

Upvotes: 2

Suresh Atta
Suresh Atta

Reputation: 122026

Just use pareseInt and convert string to int

For ex :

if(Integer.parseInt(userChoice) == computerChoice)

Make sure that the inputs are not null and formattable to int

edit : change parese to parse

Upvotes: 0

Related Questions