0

Doing a project for class and I tried to do it a different way from what the professor ended up doing. When compiling I get:

RPSLS.java:65: error: incompatible types: Object cannot be converted to int
  switch (userChoice){
     ^
1 error
With the error pointing at the start of the switch statement.

import javax.swing.JOptionPane;
import java.util.Random;
public class RPSLS {
  public static void main(String[] args) {

    //Setting choices and compChoice (for the number the computer chooses)
    int compChoice;
    final int Rock = 0;
    final int Paper = 0;
    final int Scissors = 0;
    final int Lizard = 0;
    final int Spock = 0;

    //Comp Choice
    Random generator = new Random();
    compChoice = generator.nextInt(5) + 1;

    //User Choice
    Object[] possibleValues = {
      "Rock", "Paper", "Scissors", "Lizard", "Spock"
    };

    Object userChoice = JOptionPane.showInputDialog(null,
      "Choose one", "Input",
      JOptionPane.INFORMATION_MESSAGE, null,
      possibleValues, possibleValues[0]); //Set the default option (AFIAK it doesn't matter for this kind of thing)

    System.out.println("The computer chose: " + compChoice);
    System.out.println("The computer chose: " + userChoice);

    //Set the userChoice equal to a number based on what was picked in userChoice question after both have entered choices and the choices are printed
    //Values are to be set as follows:
    //Rock 0
    //Paper 1
    //Scissors 2
    //Lizards 3
    //Spock 4
    if (userChoice == "Rock") {
      userChoice = String.valueOf(Rock);
    } else if (userChoice == "Paper") {
      userChoice = String.valueOf(Paper);
    } else if (userChoice == "Scissors") {
      userChoice = String.valueOf(Scissors);
    } else if (userChoice == "Lizard") {
      userChoice = String.valueOf(Lizard);
    } else if (userChoice == "Spock") {
      userChoice = String.valueOf(Spock);
    }





    //Determine who wins
    switch (userChoice) {
      case 0: //User chooses Rock
        if (compChoice == 0) //Rock
        {
          System.out.println("Tie, try again.");
        } else if (compChoice == 1) //Paper
        {
          System.out.println("Computer Win");
        } else if (compChoice == 2) //Scissors
        {
          System.out.println("Player Win");
        } else if (compChoice == 3) //Lizard
        {
          System.out.println("Player Win");
        } else if (compChoice == 4) //Spock
        {
          System.out.println("Computer Win");
        }
        break;


      case 1: //User chooses paper
        if (compChoice == 0) //Rock
        {
          System.out.println("Player Win");
        } else if (compChoice == 1) //Paper
        {
          System.out.println("Tie, try again.");
        } else if (compChoice == 2) //Scissors
        {
          System.out.println("Computer Win");
        } else if (compChoice == 3) //Lizard
        {
          System.out.println("Computer Win");
        } else if (compChoice == 4) //Spock
        {
          System.out.println("Player Win");
        }
        break;


      case 2: //User chooses scissors
        if (compChoice == 0) //Rock
        {
          System.out.println("Computer Win");
        } else if (compChoice == 1) //Paper
        {
          System.out.println("Player Win");
        } else if (compChoice == 2) //Scissors
        {
          System.out.println("Tie, try again.");
        } else if (compChoice == 3) //Lizard
        {
          System.out.println("Player Win");
        } else if (compChoice == 4) //Spock
        {
          System.out.println("Computer Win");
        }
        break;


      case 3: //User chooses lizard
        if (compChoice == 0) //Rock
        {
          System.out.println("Computer Win");
        } else if (compChoice == 1) //Paper
        {
          System.out.println("Player Win");
        } else if (compChoice == 2) //Scissors
        {
          System.out.println("Computer Win");
        } else if (compChoice == 3) //Lizard
        {
          System.out.println("Tie, try again.");
        } else if (compChoice == 4) //Spock
        {
          System.out.println("Player Win");
        }
        break;


      case 4: //User chooses spock
        if (compChoice == 0) //Rock
        {
          System.out.println("Player Win");
        } else if (compChoice == 1) //Paper
        {
          System.out.println("Computer Win");
        } else if (compChoice == 2) //Scissors
        {
          System.out.println("Player Win");
        } else if (compChoice == 3) //Lizard
        {
          System.out.println("Computer Win");
        } else if (compChoice == 4) //Spock
        {
          System.out.println("Tie, try again.");
        }
        break;


      default:
        System.out.println("There was an error. Please try again.");
        break;
    }
  }
}

Originally I thought I could set the string to a value based on what the option chose was. It seems I can't though and i'm not really sure how it wants me to put the switch as an int. What I think I might be able to do is, in my initial if statements, instead of putting the string equal to a value, I make an int that gets set to a value based on the choice of the user in the joption section. Any help or advice would be appreciated.

Jaskal
  • 37
  • 1
  • 9

2 Answers2

0

Java is a strongly-typed programming language, that means that you can't convert anything to int implicitly just because you want to. You must convert it appropiately, either by means of (int)userChoice or something like new Integer(userChoice) or even userChoice.toInt() (if provided). Thus...

switch((int)userChoice) { // Or whatever is the correct way for your objects to be converted to integers

...

}

Is expected to solve your problem. Anyway, you're not giving us enough information to find out what's the correct and consice way to do the conversion for your given objects.

3442
  • 8,248
  • 2
  • 19
  • 41
cagince_
  • 3
  • 5
-1

First, you cannot use an Object in a switch statement. As it tells you, you need an int. Give these different values here (like so, and use lowercase):

final int rock = 0;
final int paper = 1;
final int scissors = 2;
final int lizard = 3;
final int spock = 4;

So create another variable called something like userChoiceNumber, and change this bit (to look something like this, and I would also recommend converting the user input to lowercase, and as this is an assignment, I'll let you figure that out):

int userChoiceNumber = -1; // As a default value that you can check, or change to 0 for a valid default
if (userChoice.equals("Rock")) { // Instead of using == use equals
  userChoiceNumber = rock;
} else if (userChoice.equals("Paper")) {
  userChoiceNumber = paper;
} else if (userChoice.equals("Scissors")) {
  userChoiceNumber = scissors;
} else if (userChoice.equals("Lizard")) {
  userChoiceNumber = lizard;
} else if (userChoice.equals("Spock")) {
  userChoiceNumber = spock;
}

And then for your switch statement, you swap over to using userChoiceNumber, and can replace 0-4 with the final ints:

//Determine who wins
switch (userChoiceNumber) {
  case rock: //User chooses Rock

Edit: Changed from using == to using .equals() which handles comparisons of Strings properly.

Gliderman
  • 1,195
  • 9
  • 18