#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    1

    String showing in reverse order


    I can't figure out why the String showCard is showing the strings in reverse order:

    Code:
    run:
    Welcome to Thirty3.
    
    Would you like to play (Y/Q)? y
    
    Your cards: Three of Spades , Six of Spades , Three of Diamonds , 
    showCard : Three of DiamondsSix of SpadesThree of Spades BUILD SUCCESSFUL (total time: 2 seconds)
    Code:
    package cardgame;
    
    import java.util.Scanner;
    import java.util.Random;
    
    public class CardGame {
        
        public static void main(String[] args) {
            
            Scanner scan = new Scanner(System.in);
            Random rand = new Random();
            
            String[] cards = {"Ace", "Two", "Three", "four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
            String[] suits = {" of Hearts", " of Diamonds", " of Spades", " of Clubs"};
            
            int shuffleCards, shuffleSuits;
            
            String play, anotherCard, showCard;
            
            showCard = " ";
            
            System.out.println("Welcome to Thirty3.\n");
            
            System.out.print("Would you like to play (Y/Q)? ");
            play = scan.nextLine();
            
            if(play.equalsIgnoreCase("y"))
            {
                System.out.println();
                
                System.out.print("Your cards: ");
                for(int a=1; a<=3; a++)
                {
                    shuffleCards = rand.nextInt(cards.length-1) + 0;
                    shuffleSuits = rand.nextInt(suits.length-1) + 0;
                    
                    System.out.print(cards[shuffleCards] + suits[shuffleSuits] + " , ");         
                    
                    showCard = cards[shuffleCards].concat(suits[shuffleSuits]).concat(showCard);
                }
                
                System.out.println();
                
                System.out.print("showCard : " + showCard);
                
    //            System.out.print("Would you like another card (Y/N)? ");
    //            anotherCard = scan.nextLine();
    //            
    //            while(anotherCard.equalsIgnoreCase("y"))
    //            {
    //                System.out.print("Your cards: " + showCard);
    //                
    //                shuffleCards = rand.nextInt(cards.length-1) + 0;
    //                shuffleSuits = rand.nextInt(suits.length-1) + 0;
    //                
    //                System.out.print(cards[shuffleCards] + suits[shuffleSuits]);    
    //                
    //                System.out.println();
    //                
    //                System.out.print("Would you like another card (Y/N)? ");
    //                anotherCard = scan.nextLine();
    //            }            
            }        
        }
    }
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    In what order does the code concatenate the Strings to build showCard? Check if the logic is the opposite of what you want.
    If you want the new item at the end, put it at the end of the concatenations.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    1
    Originally Posted by NormR
    In what order does the code concatenate the Strings to build showCard? Check if the logic is the opposite of what you want.
    If you want the new item at the end, put it at the end of the concatenations.
    Thank you sir,

    I got it now.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    1
    How can I make it show the "," like in the original String?

    Code:
    run:
    Welcome to Thirty3.
    
    Would you like to play (Y/Q)? y
    
    Your cards: four of Spades , Jack of Hearts , Eight of Hearts (Total 25)
    Would you like another card (Y/N)? y
    Your cards:  four of SpadesJack of HeartsEight of HeartsQueen of Hearts
    Would you like another card (Y/N)? n
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    1
    Originally Posted by tonynsx
    How can I make it show the "," like in the original String?

    Code:
    run:
    Welcome to Thirty3.
    
    Would you like to play (Y/Q)? y
    
    Your cards: four of Spades , Jack of Hearts , Eight of Hearts (Total 25)
    Would you like another card (Y/N)? y
    Your cards:  four of SpadesJack of HeartsEight of HeartsQueen of Hearts
    Would you like another card (Y/N)? n
    Got that also, never mind
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    5
    Rep Power
    0
    code will print regular string by using-
    showCard =concat(showCard). cards[shuffleCards].concat(suits[shuffleSuits]);
    instead of-
    showCard = cards[shuffleCards].concat(suits[shuffleSuits]).concat(showCard);

IMN logo majestic logo threadwatch logo seochat tools logo