X88B88
X88B88

Reputation: 1

Return statement in a toString class

So I have a Card class that looks like this:

public class Card
{
    //instance variables
    private String faceValue; //the face value of the card
    private String suit; //the suit of the card
    String[] ranks = {"Ace", "2", "3", "4", "5", "6","7", "8", "9", "10", "Jack", "Queen", "King"};
    String[] suits = {"Clubs", "Diamonds", "Hearts", "Spades"};

/**
 * Constructor
 */
public Card(int aValue, int aSuit)
{
    faceValue = ranks[aValue];
    suit = suits[aSuit];
}

//getters
/**
 * Getter for faceValue.
 */
public String getFaceValue()
{
    return faceValue;
}

/**
 * Getter for suit.
 */
public String getSuit()
{
    return suit;
}
//end of getters

//methods
/**
 * This method returns a String representation of a Card object.
 * 
 * @param   none
 * @return  String 
 */
public String toString()
{
    return "A card: " + faceValue + " of " + suit;
}
}

And a Deck class that looks like this:

public class Deck
{
    //instance variables
    private Card[] deck;

/**
 * Constructor for objects of class Deck
 */
public Deck() 
{
    deck = new Card[52];

    int cardCount = 0; //number of cards created so far.
    for (int aSuit = 0; aSuit < 4; aSuit++ ) 
    {
        for ( int aValue = 0; aValue < 13; aValue++ ) {
            deck[cardCount] = new Test(aValue, aSuit);
            cardCount++;
        }
    }
}

/**
 * String representation.
 */
public String toString()
{
    for (int i = 0; i < 52; i++)
    {
        String v = deck[i].getFaceValue();
        String s = deck[i].getSuit();
        return "Dealt a card: " + v + " of " + s + ".";
    }
}
} 

My toString method in the deck shows an error "missing return statement". How do change the return statement while still allowing it to print the card details after every loop?

Upvotes: 0

Views: 2189

Answers (3)

Southc
Southc

Reputation: 23

if u just want to print all the cards infos, u can use

public String toString(){
    for (int i = 0; i < 52; i++)
    {
    String v = deck[i].getFaceValue();
    String s = deck[i].getSuit();
    System.out.println( "Dealt a card: " + v + " of " + s + ".");
    }
    return null;
}

Upvotes: 0

Sid
Sid

Reputation: 4995

The error is because your toString method has the return type String.
Java checks for execution paths. What if the for loop ends and never returns anything?

Although you may have hardcoded that logic, but Java compiler does not do a statistical analysis of that. Hence it must return a type String which is sure to execute if nothing else works.

This will work:

public String toString()
{
    for (int i = 0; i < 52; i++)
    {
        String v = deck[i].getFaceValue();
        String s = deck[i].getSuit();
        return "Dealt a card: " + v + " of " + s + ".";
    }
    return "";
}

But I guess what you want is this:

public String toString()
{
    StringBuilder returnValue = new StringBuilder("");
    for (int i = 0; i < 52; i++)
    {
        String v = deck[i].getFaceValue();
        String s = deck[i].getSuit();
        returnValue.append("Dealt a card: " + v + " of " + s + ".");
    }
    return returnValue.toString();
}

Upvotes: 0

Ninad Shaha
Ninad Shaha

Reputation: 81

Code you wrote will only return 0th element from deck array. it should be like:

public String toString()
{
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < 52; i++)
    {
        String v = deck[i].getFaceValue();
        String s = deck[i].getSuit();
        sb.append("Dealt a card: ").append(v).append(" of ").append(s).append(".\n");
    }
    return sb.toString();
}

Upvotes: 2

Related Questions