Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

385
Visualizações
Can I use a String in a switch statement and use .contains() with Java?

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?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

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();
}
over 4 years ago · Santiago Trujillo Relatório

0

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.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda