-1

Massive Java novice here.

I'm trying to use a previous method within another. The current goal is to find out if the two cards add up to 21. If they do return true.

Method I'm trying to reuse.

public int parseCard(String card) {
        if (card == "ace"){
            return 11;
        } else if (card == "two") {
            return 2;
        } else if (card == "three") {
            return 3;
        } else if (card == "four") {
            return 4;
        } else if (card == "five") {
            return 5;
        } else if (card == "six") {
            return 6;
        } else if (card == "seven") {
            return 7;
        } else if (card == "eight") {
            return 8;
        } else if (card == "nine") {
            return 9;
        } else if (card == "ten") {
            return 10;
        } else if (card == "jack") {
            return 10;
        } else if (card == "queen") {
            return 10;
        } else if (card == "king") {
            return 10;
        } else {
            return 0;
    }
        }

Second method ( one I need help with)

public boolean isBlackjack(String card1, String card2) { 
        parseCard(card1);
        parseCard(card2);

            if (card1 + card2 == 21)
        return true;
        
    } else {
        return false;
    }
Venom
  • 11
  • 1
  • 3
  • 2
    Your `if` is trying to compare the concatenation of two strings to a number. That will not do what you want to do, and in fact I doubt it even compiles. That said, you forgot to state what problem exactly you're having with the code you posted, if it's giving you any errors and what those errors are, or if it's producing a result that differs from what you expected. You should include that in your question. – JustAnotherDeveloper Jan 25 '22 at 17:41
  • 3
    While that is not your question, for your first method to actually work you should take a look at [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – OH GOD SPIDERS Jan 25 '22 at 17:43
  • 1
    Ah, yes. That too. And while we're at it, maybe look at using a Map or Enum, for brevity. Such a large if/else is not the nicest thing to look at. – JustAnotherDeveloper Jan 25 '22 at 17:45

1 Answers1

-1

Instead of this:

        parseCard(card1);
        parseCard(card2);

            if (card1 + card2 == 21)
        return true;
        
    } else {
        return false;
    }

You could do something like this:

public boolean isBlackjack(String card1, String card2) { 
        int c1 = parseCard(card1);
        int c2 = parseCard(card2);
        int sum = c1 + c2;
        return sum == 21;
}
Jeff Scott Brown
  • 26,804
  • 2
  • 30
  • 47
  • Many thanks. This cleared up some questions I have. – Venom Jan 25 '22 at 17:45
  • This code addresses the question as asked. A wholly separate issue is that the `parseCard` method is testing String identity, not equality, which means it won't work except for intern'd string arguments. A better approach is to replace `card == "king"` with `card.equals("king")`. An even better approach would be to use an Enum. – Jeff Scott Brown Jan 25 '22 at 17:46