Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

JAVA: Why won't it compile?

  1. Jul 20, 2008 #1
    Making a blackjack game with Java, but it won't compile :cry:. Can't figure out why.

    Code

    Code (Text):

    import java.util.*;


    class Blackjack
    {
        public static void main(String[] args)
        {
            Scanner in = new Scanner(System.in);
            Deck myDeck = new Deck();
            Player me = new Player(1000);
            Player dealer = new Player (0);
            String play = "Yes";

        while (play.equals("Yes") && me.getCash() > 0)
        {
            myDeck.shuffle();
            double bet = 0;
            System.out.print("Amount to bet");
            bet = in.nextDouble();

            while (bet > me.getCash())
            System.out.print("Your bet is too high, place another bet");
            bet = in.nextDouble();
        }

        in.nextLine();
        Card c = myDeck.getCard();
        System.out.println("Your first card is" + c);
        me.hit(c);
        c = myDeck.getCard();
        System.out.println("Dealer showing" + c);
        dealer.hit(c);
        c = myDeck.getCard();
        System.out.println("Your second card is" + c);
        me.hit(c);
        Card dealersCard = myDeck.getCard();
        dealer.hit(dealersCard);

        String answer = "";
        System.out.println("Another card?");
        answer = in.nextline();

        while (!answer.equals("No")  && me.total() <= 21)
        {
            c = myDeck.getCard();
            System.out.println("Your next card is" + c);
            me.hit(c);
            System.out.println("Another card?");
            answer = in.nextLine();
        }

        System.out.println("Dealer's other card is" + dealersCard);

        while (dealer.total() < 17)
        {
            dealersCard = myDeck.getCard();
            System.out.println("Dealer takes a" + dealersCard);
            dealer.hit(dealersCard);
        }

        if (me.total() > 21)
        {
            System.out.println("You bust");
            double newCash = me.getCash() - bet;
            me.setCash(newCash);
        }

        else if (me.total() > dealer.total())
        {
            System.out.println("You win");
            double newCash = me.getCash() + bet;
            me.setCash(newCash);
        }

        else
        {
            System.out.println("You lose");
            double newCash = me.getcash() - bet;
            me.setCash(newCash);
        }

        me.rest();
        dealer.rest();
        System.out.println("Do you still want to play?");
        play = in.nextline();
    }
    System.out.println("Game over, you have" + me.getCash());
    }
    }


    }


    class Card

    {
        private String rank;
        private String suit;
        private int value;
        public String getRank()
        {return rank;}
        public String getSuit()
        {return suit;}
        public int getValue()
        {return value;}
        public Card(String rank, String suit, int value)
        {
            this.rank = rank;
            this.suit = suit;
            this.value = value;
        }
        public String toString()
        {return rank + "of" + suit;}
    }



    class Deck
    {
        private Card[]deck;
        private int top;
        public Deck()
        {
            top = 0;
            deck = new Card[52];
            class DeckDemo
        {public static void main(String[] args)
        {
            Card[]deck = new Card[52];
            String[]ranks = {"Ace", "King", "Queen", "Jack", "Ten", "Nine",
                            "Eight", "Seven", "Six", "Five", "Four", "Three", "Two"};
            String[]suit = {"Hearts", "Clubs", "Spades", "Diamonds"};
            int[]values = {11, 10, 10, 10, 10, 9, 8, 7, 6, 5, 4, 3, 2};

            for (int i = 0; i<deck.length; i++)
                {deck [i] = new Card(ranks[i%13], suit[i%4], values[i%13]);
                System.out.println(deck[i]);
            }
        }
        public void shuffle();
        {
            Random generator = new Random();
            int i, j;

            for (i = 0; i < deck.legnth; i++)
            {
                j = generator.nextInt(deck.length);
                Card temp = deck[i];
                deck[i] = deck[j];
                deck[j] = temp;
            }
        }

        public Card getCard()
        {return deck[top++];}
    }

    class Player

    {
        Scanner in = new Scanner(System.in);
        private ArrayList<Card>hand;
        private  double cash;
        public Player(double cash)
        {
            this.cash = cash;
            hand = new ArrayList<card>();
        }
        public double getCash()
        {return cash;}
        public void setCash(double cash)
        {this.cash = cash;}
        public void hit(Card c)
        {hand.add(c);}
        public int total()
        {
            int total = 0;

            for (int i = 0; i<hand.size(); i++)
            {
                Card c= hand.get(i);
                if (c.getValue() == 11)
                {
                    total = total + c.getValue();
                    aces++;
                    }
                else total = toal + c.getValue();}

            while (total>=21 && aces > 0)
            {
                total = total - 10;
                aces = aces - 1;
            }
            return total;
        }

        public void reset()
        {hand.clear();}
    }


     

    Error messages which I have no idea how to fix

    Code (Text):


    C:\Documents and Settings\Admin\Desktop\Card.java:110: <identifier> expected
    System.out.println("Game over, you have" + me.getCash());
                      ^
    C:\Documents and Settings\Admin\Desktop\Card.java:110: illegal start of type
    System.out.println("Game over, you have" + me.getCash());
                       ^
    C:\Documents and Settings\Admin\Desktop\Card.java:110: ')' expected
    System.out.println("Game over, you have" + me.getCash());
                                            ^
    C:\Documents and Settings\Admin\Desktop\Card.java:110: ';' expected
    System.out.println("Game over, you have" + me.getCash());
                                              ^
    C:\Documents and Settings\Admin\Desktop\Card.java:110: illegal start of type
    System.out.println("Game over, you have" + me.getCash());
                                                 ^
    C:\Documents and Settings\Admin\Desktop\Card.java:110: ';' expected
    System.out.println("Game over, you have" + me.getCash());
                                                           ^
    C:\Documents and Settings\Admin\Desktop\Card.java:112: class, interface, or enum expected
    }
    ^
    C:\Documents and Settings\Admin\Desktop\Card.java:198: reached end of file while parsing
    }
    ^
    8 errors

    Tool completed with exit code 1

     
     
  2. jcsd
  3. Jul 20, 2008 #2
    Check your braces.
     
  4. Jul 21, 2008 #3

    HallsofIvy

    User Avatar
    Staff Emeritus
    Science Advisor

    One of the things I do religiously is- anytime I have a "{" I immediately write the corresponding "}" with a comment telling which "{" it is associated with and THEN fill in the code between. There is nothing harder than finding a missing brace in a long programe!
     
  5. Jul 21, 2008 #4

    DaveC426913

    User Avatar
    Gold Member

    Yep. There's definitely a bracket problem. Also, at least one typo in 'total' near the end.

    Be neat; use your indents to make your code readable. Nesting errors will stand out right away.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: JAVA: Why won't it compile?
  1. Java compile problems (Replies: 2)

  2. Java Compilation (Replies: 4)

Loading...