1

I am a Java beginner and trying to get a console blackjack to run. I created an ArrayList with cards as Strings, such as "3 Spades". Now I need to check for the values so that I dont get more than 21 for example. I have tried giving all of the Strings ints but it that isnt working for me. Is there a way to filter only the numbers out of the String and save as an int? Thanks for your help.

public ArrayList<String> Karten () {
    ArrayList<String>Herz = new ArrayList<String>(
            Arrays.asList(" 2 Herz "," 3 Herz "," 4 Herz "," 5 Herz "," 6 Herz "," 7 Herz "," 8 Herz "," 9 Herz "," 10 Herz ", " Bube Herz ", " Dame Herz ", " König Herz ", " Ass Herz "));
Federico klez Culloca
  • 22,898
  • 15
  • 55
  • 90
snakezz
  • 53
  • 5

2 Answers2

2

When you get a card, try the following:

String card = "3 Spades"
String[] cardSplit = str.split(" ");
int cardValue = Integer.parseInt(cardSplit[0]);
String cardType = cardSplit[1];
 

You can now use cardValue as an integer and add it to a total. You can also reference cardType if you need the type of card, but assuming this is Black Jack I don't think it matters much.

Let me know if this helps.

lime
  • 733
  • 3
  • 16
  • I tried this first because it is nice and easy, but I ran into the problem for "Bube Herz" where there weren´t numeric values. Thank you! – snakezz Aug 27 '20 at 08:40
  • @snakezz Yeah, making a card class is definitely the way to go. I made this assuming it would be a standard deck of cards. Not too sure what Bube Herz means but seems like some cool cards. Best of luck! – lime Aug 27 '20 at 11:39
  • Sorry I could have added that somewhere. "Herz Bube" is german for "Jack of Hearts". Sorry if that was confusing. – snakezz Aug 27 '20 at 20:50
2

You can also create a new type Card which has a numeric value field:

public class Card {
    private int value;
    private String valueLabel;
    private String shape;
    public Card(int value, String valueLabel, String shape) {
        this.value=value;
        this.valueLabel=valueLabel;
        this.shape=shape;
    }
    public int getValue() {
        return value;
    }

    public String toString() {
        return valueLabel+" "+shape;
    }
}

This also solves your problem for " Bube Herz ", " Dame Herz ", " König Herz ", " Ass Herz " which don't have a number.

Now you can build your hand as a list of Card instances:

ArrayList<Card> cards=new ArrayList<>(Arrays.<Card>asList(new Card(1,"Ass","Herz"), new Card(2,"2","Herz")));
int totalValue=cards.stream().mapToInt(c->c.getValue()).sum();
System.out.println("Your hand:"+cards);
System.out.println("has a total value of:"+totalValue);

Output:

Your hand:[Ass Herz, 2 Herz]
has a total value of:3
Conffusion
  • 4,092
  • 2
  • 13
  • 19
  • I will try to use this and create a new Card type, because I can also use it for "Bube". Only thing I could not figure out is, the "c.getValue()" method. Do I create a new method or do I use "org.javatuples" input. Thank you! – snakezz Aug 27 '20 at 08:39
  • 1
    I've added the `getValue()`method. it is just a plain POJO getter implementation – Conffusion Aug 27 '20 at 08:50
  • Thank you. Now I added all the cards for the first deck. I get "Exception in thread "main" java.lang.StackOverflowError at Card.(Card.java:22)" the issue is int this line `ArrayList cards=new ArrayList<>(Arrays.asList(new Card(2,"2","Herz"), new Card(3,"3","Herz"), new Card(4,"4","Herz") , new Card(5,"5","Herz"), new Card(6,"6","Herz"), new Card(7,"7","Herz"), new Card(8,"8","Herz"), new Card(9, "9","Herz") , new Card(10,"10","Herz"), new Card(10, "Bube","Herz"), new Card(10, "Dame", "Herz"),, new Card(11,"Ass","Herz")));` – snakezz Aug 27 '20 at 10:09
  • 1
    When I take your cards array code and remove the ',' after Dame", "Herz"), I get a valid output of 85 points. – Conffusion Aug 27 '20 at 10:18
  • Sorry, I had to cut a bit of the code because of wordlimit. The extra "," is not there in my code. I tried using only 2 cards `ArrayList cards=new ArrayList<>(Arrays.asList(new Card(2,"2","Herz"), new Card(3,"3","Herz")));` I still get the same error. – snakezz Aug 27 '20 at 10:31
  • 1
    do you have a stack trace ? I don't think your problem is in this line. StackOverflow is more related to code that loops endlesly or calls itself ? – Conffusion Aug 27 '20 at 10:38
  • I found it. Sorry for bothering, I called it the wrong way in my main method and missed something. Thanks again, you kind of saved me! – snakezz Aug 27 '20 at 10:46