0

I would like my dealer class in a BlackJack project to print the top two "cards" of an arrayList "theDeck". I am getting a compiler error (shown later) and can't figure out what it means. This is my code for a class "Deck" that makes a deck of cards, shuffles them, and puts them in an arrayList "theDeck":

 *  Compilation:  javac Deck.java
 *  Execution:    java Deck
 *  Author:       Aidan Hill
 *  Status:       Working
 *  Priority:     None
 *
 ******************************************************************************/
import java.util.ArrayList;
/******************************************************************************/


public class Deck extends Game {
   int n;
   //Deck deck = new Deck();
   String[] deckArray = new String[n];
   String[] SUITS = {
         "Diamonds", "Clubs", "Spades", "Hearts"
         };

   String[] RANKS = {
         "2", "3", "4", "5", "6", "7", "8", "9", "10",
         "Jack", "Queen", "King", "Ace"
         };

   public Deck() {
      // constructor
      n = SUITS.length * RANKS.length;
      deckArray = new String[n];
      for (int i = 0; i < RANKS.length; i++) {
         for (int j = 0; j < SUITS.length; j++) {
            deckArray[SUITS.length*i + j] = RANKS[i] + " of " + SUITS[j];
         }
      }
   }


   public String shuffleDeck() {
      // shuffle the deck
      int i;
      for (i = 0; i < n; i++) {
         int r = i + (int) ((n - i) * Math.random());
         String temporary = deckArray[r];
         deckArray[r] = deckArray[i];
         deckArray[i] = temporary;
      }
      return deckArray[i - 1];
   }


   public ArrayList<String> theDeck() {
   // save shuffled deck in an arraylist
      ArrayList<String> theDeck = new ArrayList<String>();
      for (int i = 0; i < n; i++) {
         theDeck.add(deckArray[i]);
      }
      return theDeck;
   }
}

This is my code for another class "Dealer" that will take the top two elements of arrayList "theDeck" and print them to the console:

 /******************************************************************************
 *  Compilation:  javac Dealer.java
 *  Execution:    java Dealer
 *  Author:       Aidan Hill
 *  Status:       Under Construction
 *  Priority:     !
 *
 ******************************************************************************/
import java.util.ArrayList;
import java.util.ArrayList;

/******************************************************************************/


public class Dealer extends Game {


   public ArrayList<String> dealSumCards() {
      ArrayList<String> hand = new ArrayList<String>();
      hand.add(Deck.theDeck().toString());
      System.out.println("The first hand is: " + hand);
      return hand;
   }
}

When I attempt to compile, I get this error message:

 ----jGRASP exec: javac -encoding UTF-8 -g @BlackJack_source_files_1638219710914506414jgr
Dealer.java:20: error: non-static method theDeck() cannot be referenced from a static context
      hand.add(Deck.theDeck().toString());
                   ^
1 error

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.

Whats my error? Neither method is static... Am I big stupid? I tried changing dealer to extend game instead of deck and vise versa, same problem. (If you need any more code, let me know, and I will edit the post.)
Edit: I realize now that there are many many related questions. Sorry lol. Please be patient with me, new to coding and this website.

Ampuja Kid
  • 21
  • 7
  • 1
    `theDeck()` is a non-static method. You've used `Deck.theDeck()`. Using a class name to call a method means it should be static in nature. – Robo Mop Feb 13 '20 at 04:54
  • Robo Mop how do I fix it? – Ampuja Kid Feb 13 '20 at 04:55
  • It seems your code should be `Deck object = new Deck(); object.theDeck();` – Robo Mop Feb 13 '20 at 04:55
  • If you have any related queries about your program, you can edit your question to add them, or simply ask them in this comment section. – Robo Mop Feb 13 '20 at 05:13
  • Ok. It's basically what I said in the other comments section. Here's the compiler error log: Ok yikes that didn't work. Let me try again in a different way... – Ampuja Kid Feb 13 '20 at 05:18
  • When accessing variables in an array, you can't use multiple indices like `hand[0,1]`. You'll have to print them separately, with `hand[0]+", "+hand[1]` – Robo Mop Feb 13 '20 at 05:20
  • Oh you figured it out okay sorry for that compiler mess ha ha. I will try it! – Ampuja Kid Feb 13 '20 at 05:21
  • New new error. Did what you suggested, now I'm getting an error that says `Dealer.java:21: error: array required, but ArrayList found System.out.println("The first hand is: " + hand[0] + hand[1]); ^ Dealer.java:21: error: array required, but ArrayList found System.out.println("The first hand is: " + hand[0] + hand[1]); ^ 2 errors` I hope thats readable – Ampuja Kid Feb 13 '20 at 05:24
  • Oh my god, I'm incredibly sorry. I totally forgot you were using ArrayLists and not arrays. Pretty stupid of me to be honest. Your code should be `hand.get(0)+hand.get(1)`. The square brackets fiasco was because I thought you were using normal arrays. – Robo Mop Feb 13 '20 at 05:28
  • No problem, I have no idea of the difference between the two anyways. Just started the second semester of my first ever high school programming class... I probably should have paid more attention in class at the beginning of the year so as to avoid these issues in the first place! – Ampuja Kid Feb 13 '20 at 05:29
  • New new new error: Index 1 out of bounds for length 1 – Ampuja Kid Feb 13 '20 at 05:32
  • `Do You Want To Play Blackjack? (y/n) y Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 1 out of bounds for length 1 at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70) at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248) at java.base/java.util.Objects.checkIndex(Objects.java:372) at java.base/java.util.ArrayList.get(ArrayList.java:458) at Dealer.dealSumCards(Dealer.java:21) at Game.main(Game.java:20) ` – Ampuja Kid Feb 13 '20 at 05:39
  • Might I ask, why are you making a new ArrayList for `hand`? It's only storing one object, which is the deck. Can you try: `Deck.theDeck().get(0)+Deck.theDeck().get(1)`? – Robo Mop Feb 13 '20 at 05:42
  • IT WORKED WITHOUT ISSUE!!!!!!! Thanks so much for bearing with me, this will save lots of time trouble shooting! – Ampuja Kid Feb 13 '20 at 05:46
  • You didn't even read the error message correctly. It is about a method, not a variable, and you are indeed calling it from a static context. – user207421 Feb 13 '20 at 06:14
  • Yeah, sorry about that @user207421 and wow you are a meta user! Kinda cool to see little me get seen by someone with a ~35.5 mil impact! – Ampuja Kid Feb 13 '20 at 18:18

1 Answers1

0

Deck is the name of a class. theDeck() is not a class method; you have to call it on an instance of the class.

There are two ways you could get this to work. One way would be to instantiate a Deck object, and call theDeck() on it:

Deck deck = new Deck();
hand.add(deck.theDeck().toString());

Another would be to make theDeck() static:

public static ArrayList<String> theDeck() {
// save shuffled deck in an arraylist
   ArrayList<String> theDeck = new ArrayList<String>();
   for (int i = 0; i < n; i++) {
      theDeck.add(deckArray[i]);
   }
   return theDeck;
}

This would make Deck.theDeck() work - except that n is a non-static field; you'd need to make it static or pass it in as a method parameter.

Malcolm Crum
  • 4,345
  • 4
  • 30
  • 49
  • Your first suggestion did not work, no change in compiler error message. The second one instead gave me a list of all the other non static variables I'm calling. I'll make all those static, then get back to you. – Ampuja Kid Feb 13 '20 at 04:59
  • For the first suggestion, are you sure you replaced the class `Deck` with the object `deck` where you called `theDeck()`? – Malcolm Crum Feb 13 '20 at 04:59
  • I copied your syntax for the first line exactly. I ended up getting the program to work using your second suggestion though, so I'll give you the answer credit you deserve. Thank you for your help! I will also roll back my changes and retrying your first suggestion again just to make sure. – Ampuja Kid Feb 13 '20 at 05:02
  • 1
    @Crummy You should also mention that any variables used in `theDeck()` should be static if you're gonna make the method itself static. In OP's code, `int n` is a data member, non-static. – Robo Mop Feb 13 '20 at 05:06
  • If you have time, am I allowed to open up another question for you? When I try to edit my dealSumCards method in Dealer class to print just the first two elements instead of the entire deck, tells me it expects a ) and ] – Ampuja Kid Feb 13 '20 at 05:06
  • @RoboMop I got lead on a goose chase for a minute tracking down all my non static variables ha ha! Definitely agree, but whats done is done and the info is out there now. – Ampuja Kid Feb 13 '20 at 05:08
  • @AmpujaKid If your question is scarcely related to this one, then feel free to open up a new question! Usually, separate questions that are related to the posted question are resolved in comments like these. – Robo Mop Feb 13 '20 at 05:12
  • I tried, however my last question "wasn't taken very well by the public" because I am very stupid and forgot a plus sign in my println while printing a variable, so I cant ask another question for another 6 days :D! – Ampuja Kid Feb 13 '20 at 05:14