2

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.

  • Just another thing how do I override the Arrays.toString(cards) to print out a format like for example: AC KS Also how do I only print out the complete hand once?? Thanks for the help. – FreshWaterJellyFish Aug 05 '12 at 06:23

4 Answers4

6

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++;
}
Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
  • I am just curious for my own gain here but is there a way to keep the kind of for loop he has and update the cards array that way? Would one have to keep a counter? – edhedges Aug 05 '12 at 05:59
  • Thank you. I prefer the first method but was just curious if Java gave the `card` local loop variable some sort of index attribute that could be used like `cards[card.index]`. Seeing from your answer it is necessary to keep your own counter. – edhedges Aug 05 '12 at 06:05
  • 1
    @edhedges Yeah, the for-each syntax was added primarily to work with `Iterable`s, which have no concept of an index. They also supported arrays with the same syntax (even though something completely different is happening under the hood) but if you need the index within the loop it's less awkward just to use the traditional `for`. – Paul Bellora Aug 05 '12 at 06:10
  • Wicked thanks. I didn't even think to use a normal for loop or keep track of the index. – FreshWaterJellyFish Aug 05 '12 at 06:12
  • stackoverflow.com/questions/660097/is-java-foreach-iteration-order-over-primitives-precisely-defined Even if the order is defined, I'm not sure that this is a pattern to follow. – Boris Treukhov Aug 05 '12 at 06:16
  • Just another thing how do I override the Arrays.toString(cards) to print out a format like for example: AC KS Also how do I only print out the complete hand once?? Thanks for the help. – FreshWaterJellyFish Aug 05 '12 at 06:49
  • @FreshWaterJellyFish - You can't override `Arrays.toString` since it's a static method. You could write your own method that takes a `String[]` and loops over it, building the formatted string (make sure to use `StringBuilder`). Or, if you were using a `List` instead of an array you could use something like Guava's [`Joiner`](http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/base/Joiner.html). You'll probably want to switch to a `List` at some point anyway - [some people think arrays shouldn't even be directly used anymore](http://stackoverflow.com/a/10868132) – Paul Bellora Aug 05 '12 at 14:03
0

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)

Community
  • 1
  • 1
zw324
  • 26,764
  • 16
  • 85
  • 118
0

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));

}
Bhavik Ambani
  • 6,557
  • 14
  • 55
  • 86
0

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 "";
   }    
}
PVR
  • 2,534
  • 18
  • 38