0

I am working on a program to give you a hand of cards but I'm not really sure why it's not returning the cards. This is a method from my class Builder which generates a random card:

public static String hand(){

String card = Builder();
String card2 = Builder();
String card3 = Builder();
String card4 = Builder();
String card5 = Builder();

out.println("Your hand is: ");
out.println( card );
out.println( card2 );
out.println( card3 );
out.println( card4 );
out.println( card5 );

return card;
return card2;
return card3;
return card4;
return card5;
1
  • 3
    Maybe take a small programming course; for some new perspectives, and learning features. Better than individual questions. Though you are welcome here. Commented Apr 9, 2013 at 12:39

4 Answers 4

2

I would recommend you to read "Head first core java" ASAP.
You can return only one value from method. If you still want that all variable should be set to given cards then follow below approach. In such case you need not to return even single value(see return type is void). Everything is set inside the method.

class Play {
    String card;
    String card2;
    String card3;
    String card4;
    String card5;

    public void hand() {
        this.card = builder();
        this.card2 = builder();
        this.card3 = builder();
        this.card4 = builder();
        this.card5 = builder();
    }

    private static String builder() {
        // return random card
        return null;  //temporary set to null

    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

No you cannot return more than one value.
But you can send an array of strings containing all those strings.

To declare the method, try this:

Public String[] hand() {
String card = Builder();
String card2 = Builder();
String card3 = Builder();
String card4 = Builder();
String card5 = Builder();
return new String[] {card, card2, card3, card4, card5};
}

Comments

0

You can only return one value, why don't you return a String array

return new String[] {card, card2, card3, card4, card5};

Comments

0

Return a String array:

return new String[] {card, card2, card3, card5, card5};

Edit:

To make this work, you must change the return type of your method to String[].

I recommend the Java Tutorial on defining methods.

1 Comment

It is saying I can't convert String[] to String am I doing this right public static String hand(){ String card = Builder(); String card2 = Builder(); String card3 = Builder(); String card4 = Builder(); String card5 = Builder(); return new String[] {card, card2, card3, card4, card5};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.