FreshWaterJellyFish
FreshWaterJellyFish

Reputation: 35

Object not updating when performing the switch statement in java

Not sure what I'm doing wrong here. But I want to change the card to the correct format.

For example given the card 1c change it to AC.

Here's some code I've been playing with:

public static void main(String[] args) {

    String[] cards = {"1c", "13s"};

    for (String card : cards) {

        switch (card.toUpperCase()) {
            case "1C":
                card = card.toUpperCase().replace("1C", "AC");
                break;
            case "13S":
                card = card.toUpperCase().replace("13S", "KS");
                break;
            default:
                System.out.println(Arrays.toString(cards));
        }
    }
    System.out.println(Arrays.toString(cards));

}

Any help would be great cheers.

Upvotes: 2

Views: 908

Answers (4)

PVR
PVR

Reputation: 2524

In addition to @Paul Borella's answer I would say that this is only possible with Java 7. As Switch statement does not allow String as an expression. So you should get compilation error at line

switch(card.toUpperCase())

If you want to acheive the same functionality then you can go for Enum.

public enum Cards {
 1C, 13S;

   public String replacedString(){
     case 1C : return "AC";
               break;

     case 13S : return "KS";
                break;

     default : return "";
   }    
}

Upvotes: 0

Bhavik Ambani
Bhavik Ambani

Reputation: 6657

You can do this by the following code

public static void main(String[] args) {

    String[] cards = {"1c", "13s"};

    for (int i = 0 ; i < cards.length ; i++) {

        switch (card[i].toUpperCase()) {
            case "1C":
                cards[i] = cards[i].toUpperCase().replace("1C", "AC");
                break;
            case "13S":
                cards[i] = cards[i].toUpperCase().replace("13S", "KS");
                break;
            default:
                System.out.println(Arrays.toString(cards));
        }
    }
    System.out.println(Arrays.toString(cards));

}

Upvotes: 0

Paul Bellora
Paul Bellora

Reputation: 55213

Within the loop, card is just a local variable, and reassigning it doesn't modify the array cards. An immediate fix would be to index over the array so you can reference each element directly:

for (int i = 0; i < cards.length; i++) {
    switch (cards[i].toUpperCase()) {
        case "1C":
            cards[i] = cards[i].toUpperCase().replace("1C", "AC");
            break;
        case "13S":
            cards[i] = cards[i].toUpperCase().replace("13S", "KS");
            break;
        default:
            System.out.println(Arrays.toString(cards));
    }
}

Edit: to answer edhedges' comment, one would need to keep a counter variable outside the loop in order to keep using the enhanced-for syntax:

int i = 0;
for (String card : cards) {
    switch (card.toUpperCase()) {
        case "1C":
            cards[i] = card.toUpperCase().replace("1C", "AC");
            break;
        case "13S":
            cards[i] = card.toUpperCase().replace("13S", "KS");
            break;
        default:
            System.out.println(Arrays.toString(cards));
    }
    i++;
}

Upvotes: 6

zw324
zw324

Reputation: 27180

Are you using Java 7? If you are not, you can't use Strings in cases.

See this problem and here(scroll down to Using Strings in switch Statements)

Upvotes: 0

Related Questions