Java Why Won't My Java Blackjack Game Compile?

  • Thread starter Thread starter sharkshockey
  • Start date Start date
  • Tags Tags
    Java
AI Thread Summary
The discussion revolves around issues encountered while trying to compile a Java blackjack game. The user is facing multiple compilation errors, primarily related to syntax and structure, including missing parentheses, incorrect method calls, and typos. Key points include the need to check for matching braces, as missing or mismatched braces are common sources of errors in Java. Specific errors highlighted include problems with the `System.out.println` statements and the `total` method in the Player class, which contains a typo. Suggestions for improvement emphasize the importance of maintaining clean, well-indented code to enhance readability and make debugging easier. Overall, the conversation focuses on troubleshooting compilation errors and improving code quality.
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

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