-2

I have no idea what I am doing wrong. I am writing a Java program with cards and hands classes. When I run my main program it just prints out null

import java.util.ArrayList;
public class Hand {
//Field
public String [] array;
private ArrayList<String> cards = new ArrayList<String>();
//Constructor 
public Hand(String [] array) {
    this.array = array;
}
//Methods
public void addCard(String card){
    cards.add(card);
}
public void removeCard(String card){
    cards.remove(card);
}
public static void printHand (){
System.out.print(Card.printCard());

}

public class Card {
//fields 
public static String num2rank, rank2abbrv, num2suit, suit2abbrv; 
//constructor 
public Card (String num2rank,String rank2abbrv,String num2suit,     
          String suit2abbrv){
this.num2suit = num2suit;
this.num2rank = num2rank;
this.rank2abbrv = rank2abbrv;
this.suit2abbrv = suit2abbrv;
}
//Methods 
public static String getRank(){
return num2rank;
}
public static String getSuit(){
return num2suit;
}
public static String getName(){
String result = "";
String rank = getRank();
String suit = getSuit();
result += rank + " of " + suit;
return result;

}
public static String getAbbrev(){
String result = "";
String rank = rank2abbrv;
String suit = suit2abbrv;
result += rank + suit;
return result;
}
public static String printCard(){
String results = "";
results += "Printing Hand: " + getAbbrev(); 
results += "\n" + getName();
return results;
}
}

public class main {

public static void main(String[] args) {

Hand h = new Hand((new String []{"3c", "4s","5d","6h","7h"}));
h.printHand();

}
}

These are both my hand and card classes. The rank and suit class is just a case statement of the cards and what their suit and rank are. When I run my main is when it prints out null. I want it to print out for example "7s" and "seven of hearts"

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Britt
  • 1
  • 3
  • 1
    Please include your Card class source code. – SporreKing Dec 11 '16 at 16:42
  • 3
    You need to post a well constructed [mcve]. Please go through the [help] sections on asking a question to see how to improve this question and your future questions. – Hovercraft Full Of Eels Dec 11 '16 at 16:42
  • 2
    And what "returns null"? Again, please improve this question. – Hovercraft Full Of Eels Dec 11 '16 at 16:52
  • I'm not sure you understand the function of `static` methods... – OneCricketeer Dec 11 '16 at 16:55
  • Please spend some time learning how to use the debugger. – OldProgrammer Dec 11 '16 at 16:56
  • 2
    @cricket_007: yours is a rhetorical comment I'm sure, because you know fully well that those methods shouldn't be static, and that likely he did this to (incorrectly) fix an all too common making static calls on non-static fields type error. – Hovercraft Full Of Eels Dec 11 '16 at 16:56
  • Shouldn't `printHand` loop over **all** cards in the cards list of the hand class? – OneCricketeer Dec 11 '16 at 16:56
  • I do not fully understand the static methods. Our teacher just gave examples did not really explain – Britt Dec 11 '16 at 16:58
  • Method: void printHand() Prints summary information on the hand – Britt Dec 11 '16 at 17:00
  • Britt: it's not your teacher's obligation to fully explain all -- you will probably gain much more by well directed self study and reading and should do this. First and foremost, understand the difference between static and instance. None of the methods of Card should be static, and you should not call them in a static way as you're doing from the Hand class. – Hovercraft Full Of Eels Dec 11 '16 at 17:00
  • Static methods exists per class, as normal methods exist per object. I recommend looking up more information on what the difference between classes and objects is, as well as what static means. – SporreKing Dec 11 '16 at 17:00
  • I have been reading more online, however when the teacher just gives a program to write without him even knowing java to help it is very hard to understand – Britt Dec 11 '16 at 17:02
  • But you **will** get to know it bit by bit as you study, explore and code. But again, **none** of the methods of Card should be static, and you're trying to call them in a static way from your printHand method. Please fix this, and please read up on what static means. Voting to close this question as a duplicate. – Hovercraft Full Of Eels Dec 11 '16 at 17:03
  • It's strange to have an unqualified teacher teaching, but if this is the case, you need to take it in your own hands and read until you understand it on your own. There is a lot written about this topic. Once you understand object orientation, you will understand the problem with your code. – SporreKing Dec 11 '16 at 17:05
  • @SporreKing: `"It's strange to have an unqualified teacher teaching,..."` we don't really know that as we're only privy to one side. – Hovercraft Full Of Eels Dec 11 '16 at 17:08

1 Answers1

0

Firstly, you have no instances of a Card in your code.

The Hand class has an array and a list of strings

public String [] array;
private ArrayList<String> cards = new ArrayList<String>();

You should really pick just one, and update the constructor accordingly

public Hand(String [] array) {
    this.array = array;
}

Now, if you want to use a Card, change these methods to accept one

public void addCard(Card card){
    cards.add(card);
}
public void removeCard(Card card){
    cards.remove(card);
}

(You need to change the list type, and the remove method won't work the way you think it will because of object equality which you haven't defined, but those are future problems)

I assume you want printHand() to actually print the hand, right? So you need a loop, not just printing one card.


As far as the Card class is concerned, you can pretty much entirely remove static keyword in that class - you want instance methods and variables (per object), not class-level ones (all Cards share the same value).

And , finally, you need to make a new Card(...) at some point.

Something like this (partial pseudocode)

List<Card> cards = new ArrayList<>();
Hand h = new Hand(cards);
h.addCard(new Card(...));
h.printHand();
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245