0

I am literally new into any programming. I'd like to know if there is a way to convert an integer into a string? I did some Google research but it didn't quite answer my question.

What I am trying to do is this: I have an int cardValue = (int)(Math.random() * 13 + 1);

It takes (should at least) a random value between 1 and 13. Later in the code it prints out cardValue + cardType into the console, for example "2 of Aces". What I want is that if the value of the card is 1, 11, 12 or 13 it would print out "Queen of Diamonds", for example, rather than "12 of Diamonds".

Card types are printed as follows and I know there must be a better way for this so any suggestions will be noted aswell:

if (cardType == 1){
 System.out.println("You got: " + cardValue + aces);
 }else if (cardType == 2) {
 System.out.println("You got: " + cardValue + spades);
 }else if (cardType == 3) {
 System.out.println("You got: " + cardValue + cross);
 }else {
 System.out.println("You got: " + cardValue + diamonds);
 }
}   

How do I solve this problem?

2 Answers 2

1

Prior to printing out the value of your cardValue variable, you can translate it into a human-readable result using a few simple if statements. There are four special cases to account for: Ace, Jack, Queen, King. You can handle each of these using distinct if/else-if statements. Define a new method for translating from cardValue to the text that you want to output, the cardName:

private static String getCardName(int cardValue) {
    // By default, you just output the number as is
    String cardName = "" + cardValue;

    if (cardValue == 1) {
        cardName = "Ace";
    }
    else if (cardValue == 11) {
        cardName = "Jack";
    }
    else if (cardValue == 12) {
        cardName = "Queen";
    }
    else if (cardValue == 13) {
        cardName = "King";
    }

    return cardName;
}

Then, simply use this new method getCardName(cardValue) to translate to the desired output format, and use the result in your println statements.

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

6 Comments

use a switch instead of multiple if
@Tokazio Thanks for the suggestion! That's definitely a valid alternative solution, but it introduces a new statement/syntax that the OP isn't necessarily familiar with as a beginner to programming, and one which does not seem essential to include in a solution for someone learning the basics. That's just my opinion though.
If anyone's interested, they can find more details on the switch statement here, and learn more about how it can replace multiple if/else if statements like this one.
you're right, it's just a comment to indicate this another more readable option that i think is good to learn too.
That's great advice. Thank you so much!
|
0

use a String array olding the text value at the card value index like

String[] txt = {"a","Queen"};
System.out.println(txt[1]);

will output 'Queen' (assuming 1 is your card value)

or

use a Enum for your cardType (and/or card value) and give it a text field to hold the name you want to print

Enum CardType{
    DIAMON("Diamon"),
    ACE("Ace");

    public String name;

    public CardType(String text){
        name = text;
    }

    public String toString(){
         return name;
    }
}

System.out.println(CardType.ACE);

will output 'Ace'

Comments

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.