Why Won't My Java Blackjack Game Compile?

  • Context: Java 
  • Thread starter Thread starter sharkshockey
  • Start date Start date
  • Tags Tags
    Java
Click For Summary

Discussion Overview

The discussion revolves around a Java implementation of a blackjack game that fails to compile. Participants are examining the code for syntax errors and structural issues that may be causing the compilation failure.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant suggests checking the braces in the code, implying that there may be mismatched or missing braces.
  • Another participant shares a personal strategy of immediately pairing braces with comments to avoid confusion later, indicating the importance of clear code structure.
  • A different participant notes a specific typo related to the 'total' method, suggesting that there are additional errors beyond just structural issues.
  • There is a recommendation for improving code readability through proper indentation, which could help in identifying nesting errors more easily.

Areas of Agreement / Disagreement

Participants generally agree that there are issues with the code, particularly related to braces and typos, but specific details about the errors remain contested and unresolved.

Contextual Notes

Participants have not fully explored all potential errors in the code, and some issues may depend on specific definitions or coding practices that are not universally agreed upon.

sharkshockey
Messages
15
Reaction score
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
Check your braces.
 
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!
 
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.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 13 ·
Replies
13
Views
4K
Replies
3
Views
3K
  • · Replies 4 ·
Replies
4
Views
1K