DojoOria
DojoOria

Reputation: 141

Two Missing Return Statements

import java.util.Scanner;

public class BlackJack {

static Scanner stdin = new Scanner(System.in);
static String[] deck = new String[52];
static String dealerCard = "", dealerHiddenCard = "", userCard = "", userCardTwo = "";
static int dealerCounter = 0, userCounter = 0;

public static void main (String[] args) {




    System.out.println("\n***Welcome to the grand Command Line Casino!***" +
         "\n***Today we are only playing single deck Black Jack!***" +
         "\n***But that's what we do everyday! HAHA!***\n");

    //Creates Deck of cards
    String[] suit = { "Clubs", "Diamonds", "Hearts", "Spades" };
    String[] rank = { "2", "3", "4", "5", "6", "7", "8", "9", "10",
            "Jack", "Queen", "King", "Ace" };

    for (int a=0; a < rank.length; a++) {
        for (int b=0; b < suit.length; b++){
            deck[suit.length*a + b] = (rank[a] +" of "+ suit[b]);
        }
    }

    //Assigns two cards to the dealer 
    //Finds the Integer value of each card and adds it to the dealerCounter

    //first dealer card
    System.out.println("----Dealer's Cards:----");
    System.out.println("(Dealer's First Card Placed Face Down)");
    dealerHiddenCard = randomCard(dealerHiddenCard);
    dealerCard = assignValue(dealerHiddenCard);
    dealerCounter = Integer.parseInt(dealerCard);

    //second dealer card
    dealerCard = randomCard(dealerCard);
    System.out.println(dealerCard);
    dealerCard = assignValue(dealerCard);
    dealerCounter += Integer.parseInt(dealerCard);


    //Assigns two cards to the user 
    //Finds the Integer value of each card and adds it to the userCounter

    //first user card
    System.out.println("\n----Your Cards:----");
    userCardTwo = randomCard(userCardTwo);
    System.out.println(userCardTwo);
    userCardTwo = assignValue(userCardTwo);
    userCounter = Integer.parseInt(userCardTwo);

    //second user card
    userCard = randomCard(userCard);
    System.out.println(userCard);
    userCard = assignValue(userCard);
    userCounter += Integer.parseInt(userCard);

/////// HERES ONE /////////
/////// HERES ONE /////////
    userAceHandler(userCounter);
/////// HERES ONE /////////
/////// HERES ONE /////////

        if (userCounter == 21)                {
            System.out.println("Black Jack! You Win!"); }


    hitStay();

    dealersTurn();          
}



public static String randomCard(String draw) {
//Pulls a random card from the deck

    while (true) {
        //Generates a random number between 0 and 52
        int card = (int)(Math.random()*52);
        draw = deck[card];

        //Makes sure the same card isn't drawn twice
        if (draw == "0") {
            continue;}
        else {
            deck[card] = "0";
            return draw;}
    }

}



public static String assignValue(String cardName) {
//Assigns a number value to the card

    if (cardName.contains("2"))    {
        cardName = "2";         }
    if (cardName.contains("3"))    {
        cardName = "3";         }
    if (cardName.contains("4"))    {
        cardName = "4";         }
    if (cardName.contains("5"))    {
        cardName = "5";         }
    if (cardName.contains("6"))    {
        cardName = "6";         }
    if (cardName.contains("7"))    {
        cardName = "7";         }
    if (cardName.contains("8"))    {
        cardName = "8";         }
    if (cardName.contains("9"))    {
        cardName = "9";         }
    if (cardName.contains("10"))   {
        cardName = "10";        }
    if (cardName.contains("Jack")) {
        cardName = "10";        }
    if (cardName.contains("Queen")){
        cardName = "10";        }
    if (cardName.contains("King")) {
        cardName = "10";        }
    if (cardName.contains("Ace"))  {
        cardName = "11";        }

    return cardName;
}



public static void hitStay() {
//Processes a user 'Hit or Stay' selection

    int userInput;

    System.out.println("\nHit or Stay?");
    System.out.println("(1) Hit\n(2) Stay");
    userInput = stdin.nextInt();

    if (userInput == 1){    
    //Draws another card and adds it to the userCounter
    //Decides where to go depending on new userCounter value

        userCard = randomCard(userCard);
        System.out.println("\n"+userCard);
        userCard = assignValue(userCard);
        userCounter += Integer.parseInt(userCard);

/////// HERES ONE /////////
/////// HERES ONE /////////
        userAceHandler(userCounter);
/////// HERES ONE /////////
/////// HERES ONE /////////

        System.out.print("(Current Count = "+userCounter+")");

        if (userCounter == 21)                              {
            System.out.println("\n21! Now it's the dealer's turn");  
                                         }
        if (userCounter < 21)                       {
            hitStay();                   }

        if (userCounter > 21)                       {
            System.out.println("Bust! Maybe next time");
            System.exit(1);                  }
    }   
    if (userInput == 2){                       
        System.out.println("\nAlright, dealer's turn");
        System.out.println("\n(Dealer Flips His Hidden Card Over)");
        System.out.println(dealerHiddenCard);
        System.out.println("Dealer Count = "+dealerCounter+")");    
    }

}



public static void dealersTurn() {
//processes the dealers turn after the user has decided to 'stay'

    /////// HERES ONE /////////
    /////// HERES ONE /////////
    dealerAceHandler(dealerCounter);
    /////// HERES ONE /////////
    /////// HERES ONE /////////

    if (dealerCounter > userCounter)                {
        System.out.println("\nDealer Wins! Maybe next time.");   }
        System.exit(1);

    if (dealerCounter == userCounter && dealerCounter > 17)     {
        System.out.println("\nPush! (tie game)");
        System.exit(1);                      }

    if (dealerCounter > userCounter && dealerCounter < 21 
                    && dealerCounter >= 17)     {
        System.out.println("\nDealer Wins! Maybe next time.");
        System.exit(1);                      }

    if (dealerCounter < userCounter && dealerCounter >= 17)     {
        System.out.println("\nYou Win!");
        System.exit(1);                      }

    if (dealerCounter > 21)                     {
        System.out.println("\nDealer Bust! You Win!");
        System.exit(1);                      }

    if (dealerCounter == 21)                    {
        System.out.println("\nDealer Wins! Maybe next time.");
        System.exit(1);                      }

    if (dealerCounter < userCounter || dealerCounter < 17)      {
        dealerCard = randomCard(dealerCard);
        System.out.println("\n"+dealerCard);
        dealerCard = assignValue(dealerCard);
        dealerCounter += Integer.parseInt(dealerCard);
        System.out.println("(Dealer Count = "+dealerCounter+")");

        dealersTurn();                       }
}



public static int userAceHandler(int userCounter) {
//handles the situation if an Ace is drawn for the user

    if (userCounter >= 22 && userCard == "11")          {
        userCounter = userCounter - 10; 
        return userCounter;                  }
}



public static int dealerAceHandler(int dealerCounter) {
//handles the situation if an Ace is drawn for the dealer

    if (dealerCounter >= 22 && dealerCard == "11")          {
        dealerCounter = dealerCounter - 10;
        return dealerCounter;                    }


}

}

Alright! I've spent about 10 hours making this today (its a rudimentary blackjack game) and I had it working perfectly before adding in the last two methods at the bottom of the code. They are to handle the the 'Ace' condition in my code... Unfortunately when I try to compile it's telling me that I'm missing a return statement for both. I'm only 5 weeks into my programming career and a week into my "making my own methods" career so I haven't got a clue. i thought i did it right.

I'll mark with extensive notes where I'm calling the Methods at (only 3 spots total spots for each one)

Like i said though it was working perfectly with everything else before i added these in.

Thank you so much for the help!

Upvotes: 4

Views: 82

Answers (3)

Nick
Nick

Reputation: 930

As discussed the best solution is to move the return statement

public static int dealerAceHandler(int dealerCounter) {
  if (dealerCounter >= 22 && dealerCard == "11") {
    dealerCounter = dealerCounter - 10;
  }
  return dealerCount;
}

-- original-- Since you're editing the input to the functions, maybe just make the method signature void and not return anything

Note, you would have to use an object to do this since Java is pass by reference except for primitives

public static void dealerAceHandler(Integer dealerCounter) {
  if (dealerCounter >= 22 && dealerCard == "11") {
    dealerCounter = dealerCounter - 10;
  }
}

otherwise the return is inside the if statement, so no value is returned if the condition isn't met

Upvotes: 1

Luo
Luo

Reputation: 445

The thing is, that in java methods you must ALWAYS return something. Your methods returns value only when the conditions is done. You must return something even it is not. Try to write return 0; at the end or something like that.

Upvotes: 3

Greg Hewgill
Greg Hewgill

Reputation: 994061

You do have return statements in your userAceHandler functions, but it's not enough. What if the if condition is not true? There is no return statement in that case. Java requires that you have a return statement for every possible code path out of a non-void function.

Upvotes: 9

Related Questions