Search code examples
javacounterblackjack

What am I doing wrong blackjack file counter in Java?


I am doing this exercise, the exercise says this:

*Given an input file containing 1000 random blackjack hands between 3 players (here: blackjack.txt), calculate the number of blackjacks encountered for any player in all games.

A blackjack is defined as an Ace of any suit and a 10 valued card (Jack, Queen ,King or 10) of any suit.

The input file looks like this: (as an example)

4H 5C AD JH 9C 10H

This means that player one has a 4 of hearts and a 5 of clubs; player 2 has an Ace of Diamonds and a Jack of Hearts (which counts as a blackjack); player 3 has a 9 of Clubs and a 10 of Hearts.

For the purpose of this problem, it is known that there is a standard 52 card deck which is reshuffled for each new game.*

I think I am in the correct approach, and my code is correct, but my answers fails, any hint, suggestion for getting a right answer

this is my code:

import java.io.*;

public class Problema16 {

    public static void main(String args[]) {
        File archivo = null;
        FileReader fr = null;
        BufferedReader br = null;
        int counter = 0;
        //int rest = 0;

        try {
            archivo = new File("C:\\Users\\\blackjack.txt");
            fr = new FileReader(archivo);
            br = new BufferedReader(fr);
            String linea;
            String[] linea2 = null;

            while ((linea = br.readLine()) != null) //System.out.println(linea);
            {
                linea2 = linea.split(" ");

                String a = (linea2[0]);
                String b = (linea2[1]);
                String c = (linea2[2]);
                String d = (linea2[3]);
                String e = (linea2[4]);
                String f = (linea2[5]);

                if ((a.startsWith("A") && (b.startsWith("J") || (b.startsWith("Q") || (b.startsWith("K") || (b.startsWith("10")))))) || ((a.startsWith("J") || (a.startsWith("Q") || (a.startsWith("K") || (a.startsWith("10"))))) && (b.startsWith("A")))) {
                    counter++;
                    //System.out.println(a + "" + b + "");
                } else if ((c.startsWith("A") && (d.startsWith("J") || (d.startsWith("Q") || (d.startsWith("K") || (d.startsWith("10")))))) || ((c.startsWith("J") || (c.startsWith("Q") || (c.startsWith("K") || (c.startsWith("10"))))) && (d.startsWith("A")))) {
                    counter++;
                    //System.out.println(c + "" + d + "");
                } else if ((e.startsWith("A") && (f.startsWith("J") || (f.startsWith("Q") || (f.startsWith("K") || (f.startsWith("10")))))) || ((e.startsWith("J") || (e.startsWith("Q") || (e.startsWith("K") || (e.startsWith("10"))))) && (f.startsWith("A")))) {
                    counter++;
                    //System.out.println(e + "" + f + "");
                } else {
                    //sobra++;
                }
            }

            System.out.println(counter);
            //System.out.println(sobra);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

this is the exercise online 1. My answers is 119, but is wrong.


Solution

  • Your conditions in each if are hard to read with all the braces but seem OK. Your problem lies in else if. Player #1 and Player #2 (and Player #3) can have blackjack at the same time where as your code will only count 1 of them.

    Drop the else and you should be OK.

    You can also improve the readability of your code a little. Some pointers:

    • Checking 2nd / 3rd players' hands involves the same logic as first. Use a loop.
    • Instead of explicitly checking for specific cards perhaps you can calculate the value of the hand and check if it equals 21. It'd be nice to put that logic in a separate method.
    • A simple way to calculate hand's value (assuming input is always legal) is to drop the suit (since it's irrelevant) and use a predefined map to determine card value (e.g. "J" => 10, "A" - 11, etc.)