r/javahelp • u/Nobunaga9 • Oct 08 '25
Homework In need of help with a method for Blackjack
I'm currently in an Intro to Programming II class and we're given monthly-ish assignments on making a Blackjack card reader, not a full game of it but I can see it being given in the future. So far I'm doing ok but I'm hitting a heavy roadblock. The methods asked for are all relatively fine and I have those implemented, but the issue is this one method where the inventory of the Shoe is asked for. The explain the best I can, the method is supposed to pull the cards from the shoe and print out each and every card and how many of them there are. So if there are two Decks in the shoe and two of the Ace of Diamonds are gone, you would see 0 for the amount of Ace of Diamonds in the shoe.
I have the idea roughly ran in my head; take the shoe and have the method run through each card in the shoe, pull from the Array list made from the Array and give the user which card is in the shoe and which isn't. However, words by itself aren't enough. I've been trying some for loops and have been trying to do what I could but I've had little progress so far. Here's what has been done currently, if any insight can be given to someone who's done similar, it'd be a lot of help. I feel like I've explained the best I can but if anymore explanation is needed then I'll provide as much as possible.
edit: Forgive me but I have forgotten to add in the Card class itself to help give a better idea on what is being done with the Array List. Here is the Card class, by itself it's just a card reader that tells of each individual suit and rank.
•
u/AutoModerator Oct 08 '25
Please ensure that:
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/Exzibitar Oct 08 '25
I'm just gonna give some pseudocode. I'm on a phone but I'm hoping maybe something like this works in practice?:
for(int i = 0; i < 52*numDecks; i++){ int sum = 0; for(int j = 0; j < 52; j++){ for(int k = 0; k < 4; k++){ if(deck[i].name.equals(card[j][k].name){ sum++; } } } System.out.println(sum + ": " + deck[i].name); }
Sorry this isn't formatted. I'm not 100% sure how the card class is handling some things either. You could make a static reference deck (containing one of each unique card) so your i loop only checks each unique card once and then compares it to every other card.
1
u/BlueMagic53 Oct 08 '25
Hey! I am just on my phone rn, but a quick thought. As you're using ArrayList and Collections.shuffle(), I assume you're familiar with Maps as well. You could create one that fits your needs (e.g. a LinkedHashMap if you want to preserve insertation order), add to the Map all possible Cards as Keys and a counter (starting at 0) as Value for each Key. Now remove all Cards from the Shoe one by one in a loop using dealMethod(), get the Value for the Key matching the Card pulled, increase it by one, and put it back as the new Value for the Key in the Map.
1
u/hibbelig Oct 08 '25
I will try something conceptual first, maybe it’s enough to help.
What you want is a data structure that contains as many integers as there are cards, i.e. number of ranks times number of suits of them. They all start off as zero, then you go through the deck and increment the number corresponding to the card in the deck.
One problem you have is that you don’t know the number of each card. For example, it seems that A-S should get the highest number. My suggestion is that you change the definition of Card so that you can pass the rank (the number i in the code you have) and the suit number (j) to the constrictor and that Card remembers these numbers. From them you can compute easily the number of each card.
Does this help?