Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7814299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:09:18+00:00 2026-06-02T05:09:18+00:00

I am having a problem testing blackjack java code, this is the following code:

  • 0

I am having a problem testing blackjack java code, this is the following code:

package view;

/*
////In this applet, the user plays a game of Blackjack.  The
////computer acts as the dealer.  The user plays by clicking
////"Hit!" and "Stand!" buttons.

////The programming of this applet assumes that the applet is
////set up to be about 466 pixels wide and about 346 pixels high.
////That width is just big enough to show 2 rows of 5 cards.
////The height is probably a little bigger than necessary,
////to allow for variations in the size of buttons from one platform
////to another.

*/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import model.Card;
import model.Deck;
import model.Hand;

public class GUI21 extends JApplet {

public void init() {

  // The init() method creates components and lays out the applet.
  // A BlackjackCanvas occupies the CENTER position of the layout.
  // On the bottom is a panel that holds three buttons.  The
  // BlackjackCanvas object listens for events from the buttons
  // and does all the real work of the program.

setBackground( new Color(130,50,40) );

BlackjackCanvas board = new BlackjackCanvas();
getContentPane().add(board, BorderLayout.CENTER);

JPanel buttonPanel = new JPanel();
buttonPanel.setBackground( new Color(220,200,180) );
getContentPane().add(buttonPanel, BorderLayout.SOUTH);

JButton hit = new JButton( "Hit!" );
hit.addActionListener(board);
buttonPanel.add(hit);

JButton stand = new JButton( "Stand!" );
stand.addActionListener(board);
buttonPanel.add(stand);

JButton newGame = new JButton( "New Game" );
newGame.addActionListener(board);
buttonPanel.add(newGame);

}  // end init()

public Insets getInsets() {
  // Specify how much space to leave between the edges of
  // the applet and the components it contains.  The background
  // color shows through in this border.
return new Insets(3,3,3,3);
}


// --- The remainder of this class consists of a nested class ---


class BlackjackCanvas extends JPanel implements ActionListener {

  // A nested class that displays the card game and does all the work
  // of keeping track of the state and responding to user events.

Deck deck;         // A deck of cards to be used in the game.

Hand dealerHand;   // Hand containing the dealer's cards.
Hand playerHand;   // Hand containing the user's cards.

String message;  // A message drawn on the canvas, which changes
                //    to reflect the state of the game.

boolean gameInProgress;  // Set to true when a game begins and to false
                        //   when the game ends.

Font bigFont;      // Font that will be used to display the message.
Font smallFont;    // Font that will be used to draw the cards.


BlackjackCanvas() {
     // Constructor.  Creates fonts and starts the first game.
  setBackground( new Color(0,120,0) );
  smallFont = new Font("SansSerif", Font.PLAIN, 12);
  bigFont = new Font("Serif", Font.BOLD, 14);
  doNewGame();
}


public void actionPerformed(ActionEvent evt) {
      // Respond when the user clicks on a button by calling
      // the appropriate procedure.  Note that the canvas is
      // registered as a listener in the GUI21 class.
  String command = evt.getActionCommand();
  if (command.equals("Hit!"))
     doHit();
  else if (command.equals("Stand!"))
     doStand();
  else if (command.equals("New Game"))
     doNewGame();
}


void doHit() {
      // This method is called when the user clicks the "Hit!" button.
      // First check that a game is actually in progress.  If not, give
      // an error message and exit.  Otherwise, give the user a card.
      // The game can end at this point if the user goes over 21 or
      // if the user has taken 5 cards without going over 21.
  if (gameInProgress == false) {
     message = "Click \"New Game\" to start a new game.";
     repaint();
     return;
  }
  playerHand.addCard( deck.takeCardfromDeck() );
  if ( playerHand.getScore() > 21 ) {
     message = "You've busted!  Sorry, you lose.";
     gameInProgress = false;
  }
  else if (playerHand.getScore() == 5) {
     message = "You win by taking 5 cards without going over 21.";
     gameInProgress = false;
  }
  else {
     message = "You have " + playerHand.getScore() + ".  Hit or Stand?";
  }
  repaint();
}


void doStand() {
       // This method is called when the user clicks the "Stand!" button.
       // Check whether a game is actually in progress.  If it is,
       // the game ends.  The dealer takes cards until either the
       // dealer has 5 cards or more than 16 points.  Then the 
       // winner of the game is determined.
  if (gameInProgress == false) {
     message = "Click \"New Game\" to start a new game.";
     repaint();
     return;
  }
  gameInProgress = false;
  while (dealerHand.getScore() <= 16 && dealerHand.getCardsinHand() < 5)
     dealerHand.addCard( deck.takeCardfromDeck() );
  if (dealerHand.getScore() > 21)
      message = "You win!  Dealer has busted with " + dealerHand.getCardsinHand() +".";
  else if (dealerHand.getScore() == 5)
      message = "Sorry, you lose.  Dealer took 5 cards without going over 21.";
  else if (dealerHand.getScore() > playerHand.getScore())
      message = "Sorry, you lose, " + dealerHand.getScore()
                                        + " to " + playerHand.getScore() + ".";
  else if (dealerHand.getScore() == playerHand.getScore())
      message = "Sorry, you lose.  Dealer wins on a tie.";
  else
      message = "You win, " + playerHand.getScore()
                                        + " to " + dealerHand.getScore() + "!";
  repaint();
  }


  void doNewGame() {
      // Called by the constructor, and called by actionPerformed() if
      // the use clicks the "New Game" button.  Start a new game.
      // Deal two cards to each player.  The game might end right then
      // if one of the players had blackjack.  Otherwise, gameInProgress
      // is set to true and the game begins.
  if (gameInProgress) {
          // If the current game is not over, it is an error to try
          // to start a new game.
     message = "You still have to finish this game!";
     repaint();
     return;
  }
  deck = new Deck();   // Create the deck and hands to use for this game.
  dealerHand = new Hand();
  playerHand = new Hand();
  deck.shuffle();
  dealerHand.addCard( deck.takeCardfromDeck() );  // Deal two cards to each player.
  dealerHand.addCard( deck.takeCardfromDeck() );
  playerHand.addCard( deck.takeCardfromDeck() );
  playerHand.addCard( deck.takeCardfromDeck() );
  if (dealerHand.getScore() == 21) {
      message = "Sorry, you lose.  Dealer has Blackjack.";
      gameInProgress = false;
  }
  else if (playerHand.getScore() == 21) {
      message = "You win!  You have Blackjack.";
      gameInProgress = false;
  }
  else {
      message = "You have " + playerHand.getScore() + ".  Hit or stand?";
      gameInProgress = true;
  }
  repaint();
  }  // end newGame();


  public void paintComponent(Graphics g) {
     // The paint method shows the message at the bottom of the
     // canvas, and it draws all of the dealt cards spread out
     // across the canvas.

  super.paintComponent(g); // fill with background color.

  g.setFont(bigFont);
  g.setColor(Color.green);
  g.drawString(message, 10, getSize().height - 10);

  // Draw labels for the two sets of cards.

  g.drawString("Dealer's Cards:", 10, 23);
  g.drawString("Your Cards:", 10, 153);

  // Draw dealer's cards.  Draw first card face down if
  // the game is still in progress,  It will be revealed
  // when the game ends.

  g.setFont(smallFont);
  if (gameInProgress)
     drawCard(g, null, 10, 30);
  else
     drawCard(g, dealerHand.getCard(0), 10, 30);
  for (int i = 1; i < dealerHand.getCardsinHand(); i++)
     drawCard(g, dealerHand.getCard(i), 10 + i * 90, 30);

  // Draw the user's cards.

  for (int i = 0; i < playerHand.getCardsinHand(); i++)
     drawCard(g, playerHand.getCard(i), 10 + i * 90, 160);

  }  // end paint();


  void drawCard(Graphics g, Card card, int x, int y) {
       // Draws a card as a 80 by 100 rectangle with
       // upper left corner at (x,y).  The card is drawn
       // in the graphics context g.  If card is null, then
       // a face-down card is drawn.  (The cards are 
       // rather primitive.)
  if (card == null) {  
         // Draw a face-down card
     g.setColor(Color.blue);
     g.fillRect(x,y,80,100);
     g.setColor(Color.white);
     g.drawRect(x+3,y+3,73,93);
     g.drawRect(x+4,y+4,71,91);
  }
  else {
     g.setColor(Color.white);
     g.fillRect(x,y,80,100);
     g.setColor(Color.gray);
     g.drawRect(x,y,79,99);
     g.drawRect(x+1,y+1,77,97);
     if (card.getSuit() == card.getSuit() || card.getSuit() == card.getSuit())
        g.setColor(Color.red);
     else
        g.setColor(Color.black);
     g.drawString(card.toSymbol(), x + 10, y + 30);
     g.drawString("of", x+ 10, y + 50);
     g.drawString(card.toSymbol(), x + 10, y + 70);

  }  // end drawCard()


  } // end nested class BlackjackCanvas

  }
  } // end class HighLowGUI

However I receive this following error whenever I test it:

Exception in thread "AWT-EventQueue-1" java.util.UnknownFormatConversionException: Conversion = 'i'
    at java.util.Formatter$FormatSpecifier.conversion(Unknown Source)
    at java.util.Formatter$FormatSpecifier.<init>(Unknown Source)
    at java.util.Formatter.parse(Unknown Source)
    at java.util.Formatter.format(Unknown Source)
    at java.util.Formatter.format(Unknown Source)
    at java.lang.String.format(Unknown Source)
    at model.Card.toSymbol(Card.java:38)
    at view.GUI21$BlackjackCanvas.drawCard(GUI21.java:266)
    at view.GUI21$BlackjackCanvas.paintComponent(GUI21.java:232)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JLayeredPane.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paintToOffscreen(Unknown Source)
    at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
    at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
    at javax.swing.RepaintManager.paint(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
    at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
    at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
    at java.awt.Container.paint(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)
    at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

Any suggestions would be great, thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T05:09:19+00:00Added an answer on June 2, 2026 at 5:09 am

    Pay attention to the top of the stack trace:

    Exception in thread "AWT-EventQueue-1" java.util.UnknownFormatConversionException: Conversion = 'i'
        at java.util.Formatter$FormatSpecifier.conversion(Unknown Source)
        at java.util.Formatter$FormatSpecifier.<init>(Unknown Source)
        at java.util.Formatter.parse(Unknown Source)
        at java.util.Formatter.format(Unknown Source)
        at java.util.Formatter.format(Unknown Source)
        at java.lang.String.format(Unknown Source)
        at model.Card.toSymbol(Card.java:38)
    

    That’s telling you there’s something wrong happening in the toSymbol() method of your Card class (which you haven’t posted, btw). And what’s wrong is that your toSymbol() method is apparently using a bad format string, or at least a format string that doesn’t match the data that are to be formatted using it. So look at the format string and read the Javadocs for format specifiers and figure out what you did wrong.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having problems with this code: <?php $new_value = 'testing'; $con = mysql_connect(localhost,user,pass);
I'm having the following problem: i'm developing a multiplayer game using GameKit. I'm testing
I'm having a problem testing the following model: class Bill < ActiveRecord::Base belongs_to :consignee
I'm having a problem with a line of code like: $user->has('roles', ORM::factory('role', array('name' =>
I'm having a troubling problem testing some code for and Android app. My app
I'm having problem selecting an element with an id like this <li =0f:Bactidol_Recorder.mp4>. I
I'm having a bit of a problem testing iAd on the iPhone and on
I'm having a problem with fetching some data. The tables I have (for testing
I have a very simple AsyncTask implementation example and am having problem in testing
I'm having a problem with &. Basically I haven't been able to escape this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.