Hello!

I am very new to Java and programming theory and desperately trying to improve my knowledge. This is the first program I've made without help and really would appreciate some feedback. I know there must be 1,000,000 better ways to do what I did.

Notes:

  • I want to move away from public static void main(String[] args) { } programming. How could I achieve this program using methods and OOP?
  • I am aware that the Ace should not just be = 11 and give the user the chance to dictate it's value but implementing that would not of taught me anything new.
  • The way in which I calculated the dealers & users card value is atrocious, especially because I had to re-iterate it constantly. This is the biggest problem I want to fix.

Images of console to give you an idea two runs:
http://i.imgur.com/sCRIR.jpg
http://i.imgur.com/LLvHv.jpg

import java.util.*;
public class BlackJack {


public static void main(String[] args) {

    /*
     * Scanner && User Variables
     */
    Scanner kb = new Scanner(System.in);
    int usersDecision = 0;

    /*
     * Users && Dealers Value Variables
     */
    int usersValue = 0;
    int dealersValue = 0;

    /*
     * Suit && Rank Arrays
     */
    String[] card = { "2", "3", "4", "5", "6", "7", "8", "9", "10",
            "Jack", "Queen", "King", "Ace"};
    /*
     * Array Lists Users Cards && Dealers Cards
     */
    ArrayList<String> usersCards = new ArrayList<String>();
    ArrayList<String> dealersCards = new ArrayList<String>();


    /*
     * GENERATE DEALERS FIRST CARD
     */
    for (int i = 0; i <= 0; i++) {
        int randomGenNumber = (int) (Math.random()*13);

        dealersCards.add(card[randomGenNumber]);
    }
    /*
     * Print Dealers First Card
     */
    System.out.println("The Dealer Was Dealt: " + dealersCards);


    /*
     * Deal Users Two Cards
     */
    for (int i = 0; i <= 1; i++) {

        int randomGenNumber = (int) (Math.random()*13);

        usersCards.add(card[randomGenNumber]);

    }

    /*
     * Print Users Two Cards
     */
    System.out.println("The User Was Dealt: " + usersCards);

    /*
     * Check For BlackJack
     */
    if(usersCards.contains("Ace")) {
        if(usersCards.contains("King") || usersCards.contains("Queen") || usersCards.contains("Jack") || usersCards.contains("10")){
            System.out.println("You've Got BlackJack! Congratulations, You Win!");
            System.exit(0);
        } else {
            System.out.println("You Did Not Get BlackJack!\n[1] Twist\n[2] Stick");
        }
    } else {
        System.out.println("You Did Not Get BlackJack! :( \n[1] Twist\n[2] Stick");
    }


    /*
     * Take Users Decision
     * Check Users Decision
     * While Loop
     * Switch Statement For Users Decision
     */

    int x = 0;  
    while(x==0) {

        usersDecision = kb.nextInt();

        switch (usersDecision) {
        case 1:
            System.out.println("You've Twisted - Your Cards: " + usersCards);
            System.out.println("You've Twisted - Additional Card Dealt");
            x = 0;

            /*
             * WHILE Twisting = True
             * Generate New Cards
             * Check Value of Cards
             * Bust/Twist/Stick For User
             */
            for (int i = 0; i <= 0; i++) {
                int randomGenNumber = (int) (Math.random()*13);

                usersCards.add(card[randomGenNumber]);
            }

            System.out.println(usersCards + "\n");

            /*
             * Generate Users Card Value
             */
            usersValue = 0;

            for(int i = 0; i < usersCards.size(); i++) {
                if(usersCards.get(i).equals("2")) {
                    usersValue += 2;
                } else if(usersCards.get(i).equals("3")) {
                    usersValue += 3;
                } else if(usersCards.get(i).equals("4")) {
                    usersValue += 4;
                } else if(usersCards.get(i).equals("5")) {
                    usersValue += 5;
                } else if(usersCards.get(i).equals("6")) {
                    usersValue += 6;
                } else if(usersCards.get(i).equals("7")) {
                    usersValue += 7;
                } else if(usersCards.get(i).equals("8")) {
                    usersValue += 8;
                } else if(usersCards.get(i).equals("9")) {
                    usersValue += 9;
                } else if(usersCards.get(i).equals("10")) {
                    usersValue += 10;
                } else if(usersCards.get(i).equals("Jack")) {
                    usersValue += 10;
                } else if(usersCards.get(i).equals("Queen")) {
                    usersValue += 10;
                } else if(usersCards.get(i).equals("King")) {
                    usersValue += 10;
                } else if(usersCards.get(i).equals("Ace")) {
                    usersValue += 11;
                }
            }
            /*
             * Print Users Value
             */
            System.out.println("Users Cards Value: " + usersValue + "");

            if(usersValue != 21 && usersValue <=21){
                System.out.println("You Did Not Get BlackJack!\n[1] Twist\n[2] Stick");

            } else if (usersValue == 21) {
                System.out.println("You Got BlackJack! Congratulations!");
            } else if (usersValue > 21) {
                System.out.println("You've Bust! You Lose!");
                System.exit(0);
            }

            break;

        case 2: 
            System.out.println("You've Stuck - Your Cards: " + usersCards +"\n");
            x = 1;
            /*
             * For Loop
             * Generate Users Card Value
             */
            usersValue = 0;

            for(int i = 0; i <usersCards.size(); i++) {
                if(usersCards.get(i).equals("2")) {
                    usersValue += 2;
                } else if(usersCards.get(i).equals("3")) {
                    usersValue += 3;
                } else if(usersCards.get(i).equals("4")) {
                    usersValue += 4;
                } else if(usersCards.get(i).equals("5")) {
                    usersValue += 5;
                } else if(usersCards.get(i).equals("6")) {
                    usersValue += 6;
                } else if(usersCards.get(i).equals("7")) {
                    usersValue += 7;
                } else if(usersCards.get(i).equals("8")) {
                    usersValue += 8;
                } else if(usersCards.get(i).equals("9")) {
                    usersValue += 9;
                } else if(usersCards.get(i).equals("10")) {
                    usersValue += 10;
                } else if(usersCards.get(i).equals("Jack")) {
                    usersValue += 10;
                } else if(usersCards.get(i).equals("Queen")) {
                    usersValue += 10;
                } else if(usersCards.get(i).equals("King")) {
                    usersValue += 10;
                } else if(usersCards.get(i).equals("Ace")) {
                    usersValue += 11;
                }
            }
            /*
             * Dealers Second Card 
             * &&
             * Check Dealers Value && Take Action
             */
            System.out.println("Dealing Dealers Second Card!");

            for (int i = 0; i <= 0; i++) {
                int randomGenNumber = (int) (Math.random()*13);

                dealersCards.add(card[randomGenNumber]);
            }
            System.out.println(dealersCards + "\n");

            /*
             * For Loop
             * Generate Dealers Card Value
             */
            dealersValue = 0;

            for(int i = 0; i < dealersCards.size(); i++) {
                if(dealersCards.get(i).equals("2")) {
                    dealersValue += 2;
                } else if(dealersCards.get(i).equals("3")) {
                    dealersValue += 3;
                } else if(dealersCards.get(i).equals("4")) {
                    dealersValue += 4;
                } else if(dealersCards.get(i).equals("5")) {
                    dealersValue += 5;
                } else if(dealersCards.get(i).equals("6")) {
                    dealersValue += 6;
                } else if(dealersCards.get(i).equals("7")) {
                    dealersValue += 7;
                } else if(dealersCards.get(i).equals("8")) {
                    dealersValue += 8;
                } else if(dealersCards.get(i).equals("9")) {
                    dealersValue += 9;
                } else if(dealersCards.get(i).equals("10")) {
                    dealersValue += 10;
                } else if(dealersCards.get(i).equals("Jack")) {
                    dealersValue += 10;
                } else if(dealersCards.get(i).equals("Queen")) {
                    dealersValue += 10;
                } else if(dealersCards.get(i).equals("King")) {
                    dealersValue += 10;
                } else if(dealersCards.get(i).equals("Ace")) {
                    dealersValue += 11;
                }
            }
            /*
             * Print Dealers Value
             */
            System.out.println("Dealers Cards Value: " + dealersValue + "");

            /*
             * Take Action On Dealers Value
             */
            int y = 0;
            while(y==0) {

                dealersValue = 0;   
                for(int i = 0; i < dealersCards.size(); i++) {
                    if(dealersCards.get(i).equals("2")) {
                        dealersValue += 2;
                    } else if(dealersCards.get(i).equals("3")) {
                        dealersValue += 3;
                    } else if(dealersCards.get(i).equals("4")) {
                        dealersValue += 4;
                    } else if(dealersCards.get(i).equals("5")) {
                        dealersValue += 5;
                    } else if(dealersCards.get(i).equals("6")) {
                        dealersValue += 6;
                    } else if(dealersCards.get(i).equals("7")) {
                        dealersValue += 7;
                    } else if(dealersCards.get(i).equals("8")) {
                        dealersValue += 8;
                    } else if(dealersCards.get(i).equals("9")) {
                        dealersValue += 9;
                    } else if(dealersCards.get(i).equals("10")) {
                        dealersValue += 10;
                    } else if(dealersCards.get(i).equals("Jack")) {
                        dealersValue += 10;
                    } else if(dealersCards.get(i).equals("Queen")) {
                        dealersValue += 10;
                    } else if(dealersCards.get(i).equals("King")) {
                        dealersValue += 10;
                    } else if(dealersCards.get(i).equals("Ace")) {
                        dealersValue += 11;
                    }
                }
                /*
                 * If Dealers Value:
                 * <=16 == 17 < 17 == 21 > 21
                 * 
                 */
                if(dealersValue <= 16) {
                    int randomGenNumber = (int) (Math.random()*13);
                    dealersCards.add(card[randomGenNumber]);
                    System.out.println("Dealer Has Less Than 17 - Taking Another Card\n");
                    System.out.println("Dealers Cards: " + dealersCards);
                    dealersValue = 0;

                    for(int i = 0; i < dealersCards.size(); i++) {
                        if(dealersCards.get(i).equals("2")) {
                            dealersValue += 2;
                        } else if(dealersCards.get(i).equals("3")) {
                            dealersValue += 3;
                        } else if(dealersCards.get(i).equals("4")) {
                            dealersValue += 4;
                        } else if(dealersCards.get(i).equals("5")) {
                            dealersValue += 5;
                        } else if(dealersCards.get(i).equals("6")) {
                            dealersValue += 6;
                        } else if(dealersCards.get(i).equals("7")) {
                            dealersValue += 7;
                        } else if(dealersCards.get(i).equals("8")) {
                            dealersValue += 8;
                        } else if(dealersCards.get(i).equals("9")) {
                            dealersValue += 9;
                        } else if(dealersCards.get(i).equals("10")) {
                            dealersValue += 10;
                        } else if(dealersCards.get(i).equals("Jack")) {
                            dealersValue += 10;
                        } else if(dealersCards.get(i).equals("Queen")) {
                            dealersValue += 10;
                        } else if(dealersCards.get(i).equals("King")) {
                            dealersValue += 10;
                        } else if(dealersCards.get(i).equals("Ace")) {
                            dealersValue += 11;
                        }

                    }
                    System.out.println("Dealers Cards Value: " + dealersValue + "\n");
                } 

                /*
                 * Checks dealersValue against usersValue
                 * Prints Response
                 */
                if(dealersValue == 17 ) {
                    System.out.println("Dealer Has 17 - Dealer Sticks\n");
                    y = 1;

                    if(usersValue < 17) {
                        System.out.println("You Have: " + usersValue + " You Lost");
                    } else if(usersValue == dealersValue) {
                        System.out.println("You Have: " + usersValue + " You Drew");
                    } else {
                        System.out.println("You Have: " + usersValue + " You Won!");
                    }
                }

                if(dealersValue > 17 && dealersValue < 21) {
                    System.out.println("Dealer Has: " + dealersValue + " Dealer Sticks\n" );
                    y = 1;

                    if(usersValue < 18) {
                        System.out.println("You Have: " + usersValue + " You Lost");
                    } else if(usersValue == dealersValue) {
                        System.out.println("You Have: " + usersValue + " You Drew");
                    } else {
                        System.out.println("You Have: " + usersValue + " You Won!");
                    }
                }

                if(dealersValue == 21) {
                    System.out.println("Dealer Has BlackJack\n");
                    y = 1;
                    if(usersValue == dealersValue) {
                        System.out.println("You Have: " + usersValue + " You Drew");
                    }
                }

                if(dealersValue > 21) {
                    System.out.println("Dealer Has Busted - You Win!");
                    y = 1;
                }

            }
            break;
        default:
            System.out.println("Not A Valid Selection");
        }

    };
  }
}

Any and all comments are appreciated, really want to get to grips with basic Java programming concepts.

Thanks for your time!

A couple of quick observations:
You repeat the code to get the numeric value of a card a few times - repeating code is never agood idea.
You can put all that into a method public int getValue(String card) ... that you can call whenever you want the value of a card.
In that method you can use a switch to avoid all those nested if statements.

For a more "object oriented" approach you would create a Card class that contains the name of the card and its numeric value. You'll fund that tidies up and clarifies the code quite a lot.

You repeat the code to get the numeric value of a card a few times - repeating code is never agood idea.

Would the skeleton look something along these lines?

public class BlackJack {


    public generateUserCard() {

    }
    public generateDealerCard() {

    }
    public getUserValue() {

    }

    public getDealerValue() {

    }

    public static void main(String[] args) {


    }

}

What else would I need to add to get this functional? How do I avoid the 'cannot execute static reference from a non-static reference' error that always hits me?

Yes, maybe a bit more like:

public class BlackjackGame {

   public static void main(String[] args) {
      // get out of static by creating an instance...
      new BlackjackGame();
   }

   BlackjackGame() {    // constructor calls methods to play a new Game
      dealUserCards();  // break code into easy steps
      dealDealerCards();
      ... etc
   }

   void dealUserCards()n {
      ... etc

   .. etc

   // utility methods...
   public int getValue(String card) {
      ... etc

}

Still not very object-oriented, but one step at a time...

That's great thank you! I am to make the program a total of 3 times, improving it each time. Given me a great foundation, thanks.

Two last questions:

How would you deal with user input? [1] Twist [2] Hit

Would it be along these lines?

BlackjackGame() {

    dealUserCards(); 
    getValue();
    checkBlackJack();
    dealDealerCards();

    int userInput = 0;

while loop {
    userinput = scanner.nextInt();

    switch(userInput) {

    case 1:
        dealUsersCards();
        break;
    case 2:
        dealDealersCards();
    default:
        //error
    }
 }

}

Also, you mentioned that this isn't strictly OOP - How could I achieve this? (I want to do this for the third time I write the program)

Thanks for your help.

Yes, something along those lines is OK, at least for now...

I can't do a complete into to OO here- there are lots aleady on the web, but for a card game you typically start with Card class (with members rank (Ace,2 etc) and suit). Then you have collection (eg ArrayList) of 52 Cards that you can shuffle and deal from. You have a Player class which has a collection ("hand") of Cards and methods to determine whnether that Player wants to twist or whatever. YOu may have a specialised sub-class of Player for the Dealer because the Dealer is almost but not quite the same as an ordinary player.
All this puts you in a position where you can code the top-level so its so clear that it needs no comments...

Player user1 = new Player();
Dealer dealer = new Dealer();
Deck  deck = new Deck();
deck.shuffle();
user1.takeCards(deck.deal(2));  
if (user1.twists()) then ...

That's absolutely great, thank you. You've helped me a lot!

Looking at other posts you don't get the appreciation you deserve >.<!

Thanks again, guess I will see you for my 3rd attempt in a couple of days, haha!

OK! Glad to help.

Finished my second program, is it OK to post the code here or do I need to make a new topic?

This one's marked "solved", so people may not realise there's new info. Better to start a new topic.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.