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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T21:51:05+00:00 2026-05-19T21:51:05+00:00

i have a problem with my script i have a method that return a

  • 0

i have a problem with my script

i have a method that return a card, another return the name, another the value and another the suit

public Card pickRandomCard() {
        while (rand < 6) {
        System.out.println(deckOfCards.get(rand));
        int cardValuePlayer1 = (deckOfCards.get(rand).toInt());
        currentTotal1 = cardValuePlayer1;
        String name1 = (deckOfCards.get(rand).toString());
        nameF = name1;
        String suit_1 = (deckOfCards.get(rand).suit());
        suit1 = suit_1; 
        return deckOfCards.get(rand++);
    }
        return null;

    }

    public int totalValuePlayer1() {
        return currentTotal1;
    }

    public String name1() {
        return nameF;
    }

    public String suit_1() {
        return suit1;
    }

but now, i want to create an arraylist of cards, like a hand of cards, so my ideia is create an arraylist with 5 cards, but if i did something like

    List<Card> hand = new ArrayList<Card>(); 
    Iterator<Card> iter = hand.iterator();

    while (iter.hasNext()) {
        hand.add(gr.pickRandomCard());
    }

i receive Exception in thread “main” java.lang.IndexOutOfBoundsException: Index: 2, Size: 0

i have this code in another class to call each method, suit, value of card, image, but i need to create a list to compare cards, and with the code below, it is impossible to compare a card one by one

    ActionListener one = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (gr1.getCounter1() < 5) {
                gr1.setCounter1(gr1.getCounter1() + 1);
                test1.setIcon(play1a);
                pn1.setText(Integer.toString(play2a));
                pn5.setText(play3a);
                pn50.setText(play4a);
            } else {
                pn5.setText("No more cards");
            }
        }
    };

    arraybtn[1].addActionListener(one);
    arraybtn[1].setPreferredSize(new Dimension(120, 20));

    play1a = gr.pickRandomCard().getImage();
    play2a = gr.totalValuePlayer1();
    play3a = gr.name1();
    play4a = gr.suit_1();

    arraybtn[1].setText(play3a);//change the name of button

i think the problem is that i need to create 4 list, each for correspondent method (suit, image, value), but i don’t have any idea how i can do that

thanks

  • 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-05-19T21:51:05+00:00Added an answer on May 19, 2026 at 9:51 pm

    In your code there is a flaw:

    // this iterates over existing cards
    Iterator<Card> iter = hand.iterator();
    
    // but there are no cards yet
    while (iter.hasNext() /* this will never return true */ ) {
        // so this is never executed
        hand.add(gr.pickRandomCard());
    }
    

    Try this instead:

    List<Card> hand = new ArrayList<Card>(); 
    for(int i = 0; i < 5; i++){
        hand.add(gr.pickRandomCard());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a script that constantly segfaults - the problem that I can't solve
I have a problem with a bash script. I have to use the operator
I have a small problem with a script. I want to have a default
I have a problem. I built a script to make a request to an
i have a problem, i'm working on a gallery php script and need help,
My problem is: I have a perl script which uses lot of memory (expected
I have a bash script to upload some files to my webserver. The problem
I have this .bat script which I use to maven package my application. Problem
I have problem in some JavaScript that I am writing where the Switch statement
I have problem with return statment >.< I want to store all magazine names

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.