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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:00:51+00:00 2026-05-31T06:00:51+00:00

I’m having a little trouble with some code I’m writing. Basically, I’m trying to

  • 0

I’m having a little trouble with some code I’m writing. Basically, I’m trying to “shuffle” a stack collection like a deck of cards, but for some reason, one of the temporary stacks I’m using won’t empty completely, and this causes an empty collection exception on the next run around. I traced the code and the output by hand, and the elements are being left in temporary stack 1 (the code is below). I’m really not sure why this is happening! If you have any insight on this, it’d be really helpful.

Here is a link to the problem method: http://pastebin.com/cxJCmemZ

public void shuffleCards(LinkedStack<UnoCard> deck) {
        int tempIndex;
        LinkedStack<UnoCard> tempCardStack1 = new LinkedStack<UnoCard>();
        LinkedStack<UnoCard> tempCardStack2 = new LinkedStack<UnoCard>();

        //Fisher-Yates shuffle
        for (int i = (deck.size() - 1); i >= 0; i--) {
            tempIndex = ((int)(i * Math.random()));

            System.out.println("i is: " + i);
            System.out.println("tempIndex is: " + tempIndex);

            //swap if cards are different
            if (tempIndex != i) {
                //pop face down cards up to first card onto temporary stack
                System.out.println("Popping up to first card");
                for(int j = 0; j <= tempIndex; j++) {
                    UnoCard tempCard = faceDownCards.pop();
                    System.out.println(tempCard.toString());
                    tempCardStack1.push(tempCard);
                }

                //pop face down cards up to second card onto temporary stack
                System.out.println("Popping up to second card");
                for(int j = (tempIndex + 1); j <= i; j++) {
                    UnoCard tempCard = faceDownCards.pop();
                    System.out.println(tempCard.toString());
                    tempCardStack2.push(tempCard);
                }

                //replace first card in second card position
                System.out.println("Replacing first card");
                UnoCard tempCard = tempCardStack1.pop();
                System.out.println(tempCard.toString());
                faceDownCards.push(tempCard);

                //place second card in temporary stack
                System.out.println("Transferring second card");
                tempCard = tempCardStack2.pop();
                System.out.println(tempCard.toString());
                tempCardStack1.push(tempCard);

                //replace temporary stack
                System.out.println("Replacing second stack");
                for(int j = 0; j < tempCardStack2.size(); j++) {
                    tempCard = tempCardStack2.pop();
                    System.out.println(tempCard.toString());
                    faceDownCards.push(tempCard);
                }

                //replace second card in first card position
                System.out.println("Replacing second card");
                tempCard = tempCardStack1.pop();
                System.out.println(tempCard.toString());
                faceDownCards.push(tempCard);

                //replace temporary stack
                System.out.println("Replacing first stack");
                for(int j = 0; j < tempCardStack1.size(); j++) {
                    tempCard = tempCardStack1.pop();
                    System.out.println(tempCard.toString());
                    faceDownCards.push(tempCard);
                }
            }
        }
    }
  • 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-31T06:00:52+00:00Added an answer on May 31, 2026 at 6:00 am

    if you pop a stack the size shrinks so the for loop will only run for size/2 times

    so the ending loops should really be

    while(!tempCardStack1.isEmpty()){
        tempCard = tempCardStack1.pop();
        System.out.println(tempCard.toString());
        faceDownCards.push(tempCard);
    }
    
    • 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
For some reason, after submitting a string like this Jack’s Spindle from a text
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 having trouble keeping the paragraph square between the quote marks. In firefox the
I have some data like this: 1 2 3 4 5 9 2 6
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string
I am trying to understand how to use SyndicationItem to display feed which is

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.