0

I am a high school student currently taking an introductory computer science course. In class we were assigned with creating a program that would take user input for card notation and return the full description of the card. For example, A user who inputs "AS" would see "Ace of Spades" in the terminal window. However, when my code executes, I get a "null of null" instead of "Ace of Spades" or another card notation.

public class Card

{

private String rank; 

private String suit;

private String fullRank;

private String fullSuit;

public Card(String rankAndSuit)
{
    rank = rankAndSuit.substring(0,1);


    if (rank == "A")
    {    
       fullRank = "Ace";
    } 
    else
    if  (rank == "2")
    {
        fullRank = "2"; 
    }
    else
    if (rank == "3")
    {    
        fullRank = "3";
    } 
    else
    if  (rank == "4")
    { 
        fullRank = "4"; 
    }
    else
    if (rank == "5")
    {    
        fullRank = "5";
    } 
    else
    if  (rank == "6")
    { 
        fullRank = "6"; 
    }
    else
    if (rank == "7")
    {    
        fullRank = "7";
    } 
    else
    if  (rank == "8")
    {
        fullRank = "8"; 
    }
    else
    if (rank == "9")
    {    
        fullRank = "9";
    } 
    else
    if  (rank == "10")
    {
        fullRank = "10"; 
    }
    else
    if (rank == "J")
    {    
        fullRank = "Jack";
    } 
    else
    if  (rank == "Q")
    { 
        fullRank = "Queen"; 
    }
    else
    if (rank == "K")
    {    
        fullRank = "King";
    } 


    suit = rankAndSuit.substring(1,2);


    if (suit == "D")
    {    
       fullSuit = "Diamonds";
    } 
    else
    if  (suit == "H")
    {
        fullSuit = "Hearts"; 
    }
    else
    if (suit == "S")
    {    
        fullSuit = "Spades";
    } 
    else
    if  (suit == "C")
    {      fullSuit = "Clubs"; 
    }
}

public String getCardDescription()
{
    return fullRank + " of " + fullSuit;
}

}

My Tester Class is :

public class CardTester

{

public static void main(String[] args)
{
    Card testCard = new Card("AS");
    String cardDescription = testCard.getCardDescription();

    System.out.print(cardDescription);
}

}

What is causing me to get null?

2 Answers2

0

You're comparing strings using ==:

rank == "5"

Don't. Use:

if("5".equals(rank)){

instead. The order (as opposed to if(rank.equals("5")) ensures there is no null pointer exception if the string you were comparing was ever to be null.

nanofarad
  • 40,330
  • 4
  • 86
  • 117
  • Minor nit-pick: `rank` won't ever be null, since `rank = rankAndSuit.substring(0,1);` will never return null (although it might throw an `IndexOutOfBoundsException`). There's nothing wrong with performing the comparison in that manner (I'd do it that way too), it's just that guarding against nulls isn't actually an issue in this case. – Mac Oct 23 '13 at 00:55
  • Thank you, that was my error. Ironic too because this was the subject of today's lesson! – Anthony Prudent Oct 23 '13 at 01:01
  • @Mac That is assuming the OP never uses string comparisons later in his studies in a more complex context where a string *may* be null. I prefer to answer in a way that will be appropriate to later uses of the same technique as well. – nanofarad Oct 23 '13 at 01:06
  • Like I said, I've no problem with the technique in general, and I fully support teaching it to the OP. I agree they'll likely find it useful down the track. I only meant to point out that "ensures there is no null pointer exception if `rank` itself was null" is redundant - `rank` can never be null. – Mac Oct 23 '13 at 01:27
0

You're committing a cardinal sin of string comparison in Java.

See this question: How do I compare strings in Java?

Community
  • 1
  • 1
Strelok
  • 50,229
  • 9
  • 102
  • 115