I'm not exactly sure how to word this, but I am making a blackjack game, and after the player receives 2 cards they get a y/n option to receive another card. I put the player's response (y/n) through a method which keeps bulletproofing and reprompting when invalid input is put in and then returns it back to the main code so that it will run. Here is the code for my bulletproofing method:
public class Bulletproofing {
public static String bulletproof (String another) throws IOException{
DataInputStream input = new DataInputStream(System.in);
while(!another.equalsIgnoreCase("Y")&&!another.equalsIgnoreCase("N"))
{
System.out.println("That was not a valid choice. Please type a 'Y' or 'N'");
System.out.print("Would you like another card? Y/N ");
another = input.readLine();
}
return another.toString();
}
}
The problem I am running into now is that the bulletproofng works, however after a y/n is put in, the player's card is not shown. This is the code from my main method:
System.out.print("Would you like to get another card? Y/N ");
String another = input.readLine();
Bulletproofing.bulletproof(another);
//player says yes
while(another.equalsIgnoreCase("Y") & playerTotal <=21)
{
//get card
System.out.print(card[deck.get(nextCard)]);
value = card[deck.get(nextCard)].getDenom();
if(value==11 || value == 12 || value== 13)
{
value = 10;
}
playerTotal+=value;
//show stats
System.out.println();
System.out.println("Player total " + playerTotal);
nextCard++;
System.out.println();
//something wrong //incase player goes over they don't have the choice to get another card
if(playerTotal< 21)
{
System.out.print("Player: Another card? Y/N ");
another = input.readLine();
Bulletproofing.bulletproof(another);
}
else
{
another.equals("N");
}
count ++;
}
When the player for types invalid input, the program asks for the player to input again, but then when they do a y or n, that's when the program doesn't output how I wanted it to. I expected the output to be something like:
Would you like another card? Y/N y
7 Diamonds
Player total: 25
Dealer:
8 Hearts
Dealer total: 18
Dealer Wins.
However, in the specific case where I for example put "i" and then get reprompted and put "y", I would get an output like:
Would you like another card? Y/N i
Input invalid please input again:
Would you like another card? Y/N y
Dealer:
8 Hearts
Dealer total: 18
Dealer Wins.
Here, the player's card is not outputted at all and I am a little bit confused at how to fix this.