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 7888791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:55:34+00:00 2026-06-03T05:55:34+00:00

Main Class: public class Blackjack { static Deck D; static Hand P; public static

  • 0

Main Class:

public class Blackjack {

static Deck D;
static Hand P;
public static void main(String[] args) {
    init();
}

private static void init() {
    D=new Deck();
    P=new Hand(D);
    startGame();
}

private static void startGame() {
    System.out.print("Your cards are: "+P.H.get(0)+", "+P.H.get(1));
}

}

Hand Class:

import java.util.ArrayList;
import java.util.Random;


public class Hand {
ArrayList<String> H;
Random R;
public Hand(Deck D){
    R=new Random();
    H=new ArrayList<String>();
    int C=R.nextInt(D.getP().length);
    H.add(D.getP()[C]);
    D.removeFromDeck(C);
    int E=R.nextInt(D.getP().length);
    H.add(D.getP()[E]);
    D.removeFromDeck(E);
}

}

Deck Class

public class Deck {
String[] P;
public Deck(){
    P=new String[52];
    String Suit="Default";
    int C=0;
    for(int A=1;A<=4;A++){
        switch(A){
        case 1:
            Suit="Hearts";
            break;
        case 2:
            Suit="Diamonds";
            break;
        case 3:
            Suit="Clubs";
            break;
        case 4:
            Suit="Spades";
            break;
        }
        for(int B=1;B<=13;B++){
            if(B>10){
                switch(B){
                case 11: P[C]="Joker of "+Suit; 
                break;
                case 12: P[C]="Queen of "+Suit;
                break;
                case 13: P[C]="King of "+Suit;
                break;
                }
            }else{
                P[C]=B+" of "+Suit;
            }
        }
    }
}
public void setP(String[] p) {
    P = p;
}
public String[] getP() {
    return P;
}
public void removeFromDeck(int C){
    System.arraycopy(P, C + 1, P, C,
            P.length - C - 1);
}

}

When I compile and run this code, it prints that my 2 cards are null, null. I have looked through the code and can’t seem to find my error. Maybe you can? TY for any help you give me.

Edit: It is now returning spades only, could someone please help?

  • 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-03T05:55:35+00:00Added an answer on June 3, 2026 at 5:55 am

    Both your loops have conditions the wrong way round:

    for(int A=1;A>=4;A++)
    
       ...
       for(int B=1;B>=13;B++)
    

    Neither of these will execute any iterations. They should be:

    for(int A=1;A<=4;A++)
    
       ...
       for(int B=1;B<=13;B++)
    

    Additionally, you’re not allocating enough size:

    P=new String[51];
    

    There are 52 cards in a back, not 51.

    EDIT: As noted in comments, you’ll always get spades with the code as posted, because your switch statement to select the suit doesn’t have any breaks in. I would have expected this to result in a compile-time warning. You really should pay attention to warnings. You can add break statements like this:

    switch(A) {
        case 1:
            S = "Hearts";
            break;
        case 2:
            S = "Diamonds";
            break;
        case 3:
            S = "Clubs";
            break;
        case 4:
            S = "Spades";
            break;
    }
    

    Finally, I would strongly advise you to use more meaningful variable names, preferrably following Java naming conventions. (There are actually various other changes I’d make to your code, but start off with those.)

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

Sidebar

Related Questions

public class Main { public static void main(String[] args){ Class2 class2Object = new Class2();
class D { public static void main(String args[]) { Integer b2=128; Integer b3=128; System.out.println(b2==b3);
in my main class public static void main(String[] args) { CSVWriter writer = new
package GC; import java.util.Scanner; public class main { public static void main(String args[]) {
public class Test { public static void main(String[] args) { } } class Outer
public class doublePrecision { public static void main(String[] args) { double total = 0;
public class WrapperTest { public static void main(String[] args) { Integer i = 100;
Given the following code : import java.io.*; public class Main { public static void
I know why the following code doesn't compile: public class Main { public static
I have a compiled java class: Echo.class public class Echo { public static void

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.