Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Why am I getting this output?

  1. #1
    Junior Member
    Join Date
    May 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?

    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...


  2. #2
    Member
    Join Date
    Jul 2010
    Posts
    45
    Thanks
    10
    Thanked 3 Times in 3 Posts

    Default 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?

  3. #3
    Junior Member
    Join Date
    May 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why am I getting this output?

    A lot, yeah. Now my output is as follows:

    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.

  4. #4
    Member
    Join Date
    Jul 2010
    Posts
    45
    Thanks
    10
    Thanked 3 Times in 3 Posts

    Default 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.

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default 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:
    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.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.

Similar Threads

  1. need help with output.
    By VictorCampudoni in forum What's Wrong With My Code?
    Replies: 0
    Last Post: June 24th, 2010, 11:25 PM
  2. duplicating output
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: January 3rd, 2010, 01:11 AM
  3. non well-formed output
    By luca.santaniello in forum Web Frameworks
    Replies: 1
    Last Post: November 19th, 2009, 03:52 AM
  4. OutPut.
    By chronoz13 in forum Java Theory & Questions
    Replies: 3
    Last Post: August 29th, 2009, 10:54 AM
  5. [SOLVED] Java program error in displaying Output
    By crazydeo in forum AWT / Java Swing
    Replies: 9
    Last Post: May 14th, 2008, 10:42 AM