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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:31:57+00:00 2026-05-31T04:31:57+00:00

I am trying to search a hand of cards (a queue) by inputting a

  • 0

I am trying to search a “hand of cards” (a queue) by inputting a search card, comparing the values (colour and rank) of the search card to the current card in the hand, and if a match is found, outputting the matching card and changing the “mood” of the current player. If no match is found and the entire hand has not been searched, then the current card is dequeued, enqueued at the back of the hand, and the method is run again. If the entire hand has been searched (leading back to the first card of the hand), then a “dummy card” is to be returned.
My problem is that, if a matching card is not found, then a StackOverflowError appears. I do not know where this is coming from, but I presume it has to do with the enqueuing/dequeuing of the cards and recursiveness of the method (toward the end). If anyone can help, I would appreciate it. If you need more code or more info, just ask.

NEW CODE:

(at top of class)
int counter = 1;

....

/**
 * Method that checks a player's hand for matching cards
 * @param searchCard card to search for in player's hand
 * @return card in player's hand if found, dummy card otherwise
 */
public UnoCard getMatch(UnoCard searchCard) {
    UnoCard foundCard;
    UnoCard currentCard = cards.first();
    UnoCard noCard = new UnoCard('N', 0);

    //check if colours of cards match
    if (currentCard.getColour() == searchCard.getColour()) {
        //set mood, remove card from hand, and return matching card
        setMood("Colour");
        foundCard = currentCard;
        cards.dequeue();
        return foundCard;
    //check if ranks of cards match
    } else if (currentCard.getRank() == searchCard.getRank()) {
        //set mood, remove card from hand, and return matching card
        setMood("Rank");            
        foundCard = currentCard;
        cards.dequeue();
        return foundCard;
    //if no match is found
    } else {
        //check if end of hand has been reached
        if (counter == cards.size()) {
            //set mood and return dummy card
            setMood("No match");
            counter = 1;
            return noCard;
        } else {
            //place card at back of hand and search again with next card in hand
            counter++;
            System.out.println("Counter is: " + counter);
            cards.dequeue();
            cards.enqueue(currentCard);
            return getMatch(searchCard);
        }
    }
}
  • 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-31T04:31:58+00:00Added an answer on May 31, 2026 at 4:31 am

    I think that you should have something like this:

    public UnoCard getMatch(UnoCard searchCard) {
        return getMatch(searchCard, 0);
    }
    
    private UnoCard getMatch(UnoCard searchCard, int counter) {
        //int counter = 0
        ... the rest of your code
    
        // Recursion at the end
        getMatch(searchCard, counter);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to place a search text box on the right hand side
I'm trying my hand at creating an AJAX search and having some difficulty. Here's
While trying to search with id sorting (and paginating) im getting from the log:
Im trying to search two of the fields in my table. (Database table contains
I am trying to search for files which matches following expression with sed [a-zA-Z0-9]{1,10}\s{1,5}\
I'm trying to search through many documents and find all instances where an html
I am trying to search a MongoDB database filtering on a specific data item.
I'm trying to search through a Word document and replace a specific string (Using
I am trying to search for multiple strings within a larger string and if
So I'm trying to search through a model through a form. Basically my end

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.