Why Won't My Java Blackjack Game Compile?

  • Java
  • Thread starter sharkshockey
  • Start date
  • Tags
    Java
In summary: For example, your Deck class has a semi-colon after the method declaration for 'shuffle' and before the method body. That is a syntax error.Your DeckDemo class is a nested class, and it's not closed with a "}". I'm not sure if that's what you wanted; if not, remove the nested class and the DeckDemo method from Deck, and create a new class for it.Your Player class has a typo in the 'rest' method - it should be 'reset'. Also, you should declare your 'aces' variable (as an int) at the beginning of the method, otherwise the method will cause an error as there is no 'aces' variable defined before the while loop.
  • #1
sharkshockey
16
0
Making a blackjack game with Java, but it won't compile :cry:. Can't figure out why.

Code

Code:
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:
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
 
Technology news on Phys.org
  • #2
Check your braces.
 
  • #3
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!
 
  • #4
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.
 

FAQ: Why Won't My Java Blackjack Game Compile?

Why am I getting a "cannot find symbol" error when trying to compile my Java code?

This error occurs when the compiler cannot find a declared variable or class in your code. Make sure you have imported all necessary packages and have correctly declared and initialized all variables.

How do I fix a "missing return statement" error when compiling my Java program?

This error occurs when your code is missing a return statement in a method that is supposed to return a value. Make sure all paths in the method have a return statement or add a default return statement at the end of the method.

I am getting a "class, interface, or enum expected" error when compiling my Java code. What does this mean?

This error occurs when there is a missing curly brace or semicolon in your code. Check your code for any missing punctuation and make sure all opening brackets have a corresponding closing bracket.

Why am I getting a "cannot find symbol" error when trying to use a method from another class in my Java program?

This error occurs when the compiler cannot find the method you are trying to use. Make sure you have imported the class where the method is located and check for any spelling errors in the method name.

How do I fix a "java.lang.NullPointerException" error when compiling my Java code?

This error occurs when your code is trying to access a null object or variable. Check your code for any uninitialized variables or objects and make sure they are properly assigned a value before being used.

Similar threads

Replies
1
Views
2K
Replies
2
Views
2K
Replies
3
Views
1K
Replies
7
Views
2K
Replies
3
Views
3K
Replies
13
Views
4K
Back
Top