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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:17:54+00:00 2026-06-13T08:17:54+00:00

I’m trying to learn Java and I wanted to make a very simple class

  • 0

I’m trying to learn Java and I wanted to make a very simple class that would randomly select 5 cards from a randomly generated deck of cards. I’m running into something that I feel should be a very simple issue to solve. Also, this is for a lab at the University I attend so if you are able to guide me without giving me blocks of code, that would be preferable.

This is the error I receive, and I understand why I receive it:

The type of the expression must be an array type but it resolved to Deck

Here is my code:

public static void main(String[] args) {
    System.out.println(select(5));
}

public static Card[] select(int k)
{
    Random rand = new Random(52);
    Deck deck = new Deck(52);
    Card[] hand = new Card[5];
    for (int j = 0; j < 5; j += 1)
    {
        int index = rand.nextInt(52-j);
        hand[j] = deck[index];
    }
    return hand;
}

The Deck.java and Card.java classes were provided by my instructor (who is unavailable during lab time).

I also recently realized this code is not going to do for me what I want it to do, however I still need to figure out the error I have above. If you wanted to help with the other issue I have, feel free to answer my explanation below but it’s not the reason I am here.

I’m wanting to randomly select 5 cards. Let’s say that a randomly selected card is at index 27 of my deck object. I want to then move that card to index 51 and repeat this 4 times. That way the last five cards of my deck object were all randomly selected and it’s impossible for them to be selected twice. I am thinking the easiest approach (which I have not attempted yet) would be to create a variable that holds the value of one of my deck indices so I can swap them around. Would any of you agree?

Any help is greatly appreciated!

  • 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-13T08:17:56+00:00Added an answer on June 13, 2026 at 8:17 am

    You have your deck as reference of class Deck pointing to an instance: –

    Deck deck = new Deck(52);
    

    So, you can’t access it on index like array: –

    deck[index];  // Cannot do this on a reference pointing to object of `Deck`
    

    I think you probably need to give some method in your class like get(index) and access it like: – deck.get(index)

    Or may be you wanted to declare your deck as: –

    Deck[] deck = new Deck[52];
    

    then that (deck[index])would work.

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

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
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
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.