Home > Mobile >  Can I use a String in a switch statement and use .contains() with Java?
Can I use a String in a switch statement and use .contains() with Java?

Time:12-10

I am trying to write a program that played a chopped-down version of the card game Spades, and i would like to make my code more efficient by using a switch statement instead of a repeated if/else if/else. I need the function to take in a string (userCard), which looks like "Spades, 2" or "Diamonds, 6". I need it to save the number at the end of the string into an int datatype. Below is the working if/else if/else statements.

public static int getUserValue(String userCard) {
    int userValue = 0;

    if (userCard.contains("4")) {
        userValue = 4;
    } else if (userCard.contains("5")) {
        userValue = 5;
    } else if (userCard.contains("6")) {
        userValue = 6;
    } else if (userCard.contains("7")) {
        userValue = 7;
    } else if (userCard.contains("8")) {
        userValue = 8;
    } else if (userCard.contains("9")) {
        userValue = 9;
    } else if (userCard.contains("10")) {
        userValue = 10;
    } else if (userCard.contains("11")) {
        userValue = 11;
    } else if (userCard.contains("12")) {
        userValue = 12;
    } else if (userCard.contains("13")) {
        userValue = 13;
    } else if (userCard.contains("1")) {
        userValue = 1;
    } else if (userCard.contains("2")) {
        userValue = 2;
    } else if (userCard.contains("3")) {
        userValue = 3;
    }
    
    return userValue;
}

So I thought I could do something like

public static int getUserValue(String userCard) {
    int userValue = 0;

    
    switch (userCard){
        case userCard.contains("4"):
        userValue = 4;
        break;
    }
//etc, goes up to 13 then evaluates if 1, 2, 3.


    return userValue;
}

It gives an error that I can't convert from boolean to String. Is there any way I can change this or type cast this to work the way I want?

CodePudding user response:

You should use instead something like a UserCard class:

  public class UserCard {
    private Suit _suit;
    private int _value;
    
    // Constructor
    // getter   setter
  }
  
  public enum Suit {
    DIAMONDS, HEARTS, SPADES, CLUBS;
  }

That way you don't even need the switch because you already have that value you want to extract. Your getUserValue is simply:

public static int getUserValue(UserCard userCard) {
  return userCard.getValue();
}

CodePudding user response:

To answer the question barely as it is asked, no, you can't use switch as you would like to in your example.

The cases of a switch have to be constants. You may use literal values, static final fields, or operators to combine them; but not call a method. So, for example, case 1 2 is valid as well as case A B, even when A and B are String.

However, there is a better way to extract the value from your string, by using String.indexOf, String.substring and Integer.parseInt, or regular expressions for more complex things. Or, even better, follow ohter answers which suggest to create a card class.

  • Related