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

  • Home
  • SEARCH
  • 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 8059105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:34:02+00:00 2026-06-05T09:34:02+00:00

I’m a fairly noobish guy trying to learn Java, and I’m having a little

  • 0

I’m a fairly noobish guy trying to learn Java, and I’m having a little trouble completing a task I gave myself. Basicaly I’m trying to do the exercises at the end of this page.

I managed to finish it with three classes. The Card:

public class Card {

public int nRank; // Used later
public int maxRank = 13; //The max number of Ranks
public int nSuit; // Used later
public int maxSuit = 4; // Max number of suits


//Associate both rank and suit numbers with strings

public String[] ranks = new String[maxRank - 1];
{
    ranks[0] = "two";
    ranks[1] = "three";
    ranks[2] = "four";
    ranks[3] = "five";
    ranks[4] = "six";
    ranks[5] = "seven";
    ranks[6] = "eight";
    ranks[7] = "nine";
    ranks[8] = "ten";
    ranks[9] = "Jack";
    ranks[10] = "Queen";
    ranks[11] = "King";
    ranks[12] = "Ace";

    }

public String[] suits = new String[maxSuit - 1];
{
    suits[0] = "Clubs";
    suits[1] = "Diamonds";
    suits[2] = "Spades";
    suits[3] = "Hearts";
    }

public String suit = suits[nSuit]; //The suit string of the card whose suit number is nSuit
public String rank = ranks[nRank]; //Same but with ranks

//Constructor for the Card object, with two arguments, x for rank, y for suit
public Card(int x,int y){
    this.nRank = x;
    this.nSuit = y;
}

//method to get which card it is in a string
public String whatCard(){
    return rank + " of " + suit;
}
}

The Deck:

    public class Deck {

public static int nRanks = 13; //number of ranks
public static int nSuits = 4; // number of suits
public static int nCard = nRanks * nSuits; // number of cards

Card[] deck = new Card[nCard -1]; //new array called deck to store all the cards 
int h = 0; //a variable to control the place of each card in the array
//constructor for the Deck
public Deck() {

while(h < 52){ // loop until there are 52 cards

// cycles through all the possible combinations between i(ranks) and j(suits) and creates a card with each
for(int i = 1; i <= nRanks; i++){

    for(int j = 1; j <= nSuits; j++){

        deck[h] = new Card(i,j); // creation of the card
        h++; // adds 1 to to h so the program knows how many cards are there
        }
    }

}
}
//method for getting a card depending on its position in the array(x)
    public Card getCard(int x){
    return deck[x-1];   
}
}

And the displayer of the cards/deck, which I called Shuffle:

public class Shuffle {

public static void main(String[] args){
        Deck newDeck = new Deck(); // creates a new Deck object
        //loops through all the cards in the deck
    for(int i = 0; i < Deck.nCard; i++){
        System.out.println(newDeck.getCard(i).whatCard()); // prints each card
    }

}

}

Although eclipse doesn’t notice any errors in the code, when I try to compile, I’m shown this:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 12
    at Card.<init>(Card.java:26)
    at Deck.<init>(Deck.java:20)
    at Shuffle.main(Shuffle.java:5)

What have I missed?

  • 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-05T09:34:05+00:00Added an answer on June 5, 2026 at 9:34 am

    ranks[] and suits[] have both indexes from 0 to 12.

    you’re trying to access from 1 to 13 (in your for loop)

    Use this code :

    for(int i = 0; i < nRanks; i++){
    
        for(int j = 0; j < nSuits; j++){
    
            deck[h] = new Card(i,j); // creation of the card
            h++; // adds 1 to to h so the program knows how many cards are there
        }
    }
    

    EDIT :

    I noticed another error, the one who generate the out of bounds :
    In your Card class, change this lines :

    public String[] ranks = new String[maxRank - 1];
    public String[] suits = new String[maxSuit - 1];
    

    by

    public String[] ranks = new String[maxRank];
    public String[] suits = new String[maxSuit];
    

    When you create an array, you don’t specify the last index, but the place avaible. So if you wanna put 13 values, specify new String[13].


    EDIT : the full Deck class :

    public class Deck {
    
       public static int nRanks = 13;  
       public static int nSuits = 4; 
       public static int nCard = nRanks * nSuits; 
    
       Card[] deck = new Card[nCard]; //nCard indexes, not nCard - 1
    
       public Deck() {
          //remove the while, double loop useless
          for(int i = 0; i < nRanks; i++){
             for(int j = 0; j < nSuits; j++){
                deck[j * nRanks + i] = new Card(i,j);
             }
          }
       }
    
       public Card getCard(int x){
          return deck[x-1];   
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

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.