Why am I getting this output?
Hi guys,
In this program for school (it's part of a bigger problem where I have to shuffle a deck)... I have to store every possible playing-card combination in an ArrayList. Now, I am a fairly new programmer, but honestly, the output here baffles me. What am I doing wrong?
Code :
package fifth_class;
import java.util.ArrayList;
public class CardShufflerTester {
public static void main(String[] args){
ArrayList<String> cardDeck = new ArrayList();
cardDeck = initDeck();
for(int i=0;i<cardDeck.size();i++){
System.out.println(cardDeck.get(i).toString());
}
}
public static ArrayList<String> initDeck(){
ArrayList<String> deck = new ArrayList();
for(int i=1;i<14;i++){
String card = new String();
card = "";
switch(i){
case 1: card = card.concat("Two ");
case 2: card = card.concat("Three ");
case 3: card = card.concat("Four ");
case 4: card = card.concat("Five ");
case 5: card = card.concat("Six ");
case 6: card = card.concat("Seven ");
case 7: card = card.concat("Eight ");
case 8: card = card.concat("Nine ");
case 9: card = card.concat("Ten ");
case 10: card = card.concat("Jack ");
case 11: card = card.concat("Queen ");
case 12: card = card.concat("King ");
case 13: card = card.concat("Ace ");
}
for(int j=1;j<5;j++){
switch(j){
case 1: card = card.concat("of Hearts");
case 2: card = card.concat("of Diamonds");
case 3: card = card.concat("of Clubs");
case 4: card = card.concat("of Spades");
}
}
deck.add(card);
card = "";
}
return deck;
}
}
Thanks for any help...
Re: Why am I getting this output?
If you use switch/case you should add a break after every case, otherwise it will fall through to the other cases.
switch(i){
case 1: card = card.concat("Two "); break;
case 2: card = card.concat("Three "); break;
case 3: card = card.concat("Four "); break;
Does that help at all?
Re: Why am I getting this output?
A lot, yeah. Now my output is as follows:
Code :
Two of Heartsof Diamondsof Clubsof Spades
Three of Heartsof Diamondsof Clubsof Spades
Four of Heartsof Diamondsof Clubsof Spades
Five of Heartsof Diamondsof Clubsof Spades
Six of Heartsof Diamondsof Clubsof Spades
Seven of Heartsof Diamondsof Clubsof Spades
Eight of Heartsof Diamondsof Clubsof Spades
Nine of Heartsof Diamondsof Clubsof Spades
Ten of Heartsof Diamondsof Clubsof Spades
Jack of Heartsof Diamondsof Clubsof Spades
Queen of Heartsof Diamondsof Clubsof Spades
King of Heartsof Diamondsof Clubsof Spades
Ace of Heartsof Diamondsof Clubsof Spades
I see what the problem is, but I'm not entirely sure how to fix it.
Re: Why am I getting this output?
And you added the breaks for the second switch statement also?
switch(j){
case 1: card = card.concat("of Hearts"); break;
case 2: card = card.concat("of Diamonds"); break;
...
If so, please post the complete code you have now.
Re: Why am I getting this output?
I havent played around with switches, but I have a possible suggestion. What could be happening is the break statement is breaking the switch, not the loop. So we want to break the loop. This means we need to label the loop and tell the break statement to break it.
So, breaking the outer loop would look like this:
Code java:
OurLoop:
for(int j=1;j<5;j++){
switch(j){
case 1: card = card.concat("of Hearts");break OurLoop;
case 2: card = card.concat("of Diamonds");break OurLoop;
case 3: card = card.concat("of Clubs");break OurLoop;
case 4: card = card.concat("of Spades");break OurLoop;
}
}
The statement before the FOR loop (OurLoop:) labels our loop. The statement break OurLoop; tells the break statement which loop we are breaking. This statement indicates to break OurLoop.
Re: Why am I getting this output?
In stead of breaking out of the loop, just get rid of the loop.
But that's not the problem. The looping is wrong.
I suppose the program is to produce:
Two of Clubs
Two of Diamonds
Two of Hearts
...
When is the value to be saved available to be added to the collection?
When should the value of the rank be set and changed?
The OP needs to add some println() to show the values of card as they are generated and changed to see how and where to set card to the values needed.