A method not being resolved?
hey guys. i'm self learning java and i found a code of a blackjack game online. whoever made it did a great job on it. it has 5 classes. 4 of the classes work except for one. basically the game is trying to make the user give an option to hit or stand. however, this line userAction = Character.toUpperCase(reader.getChar()); is not functioning correctly. It says that 'reader' cannot be resolved. What does that mean and what can I do to fix it? do i use a scanner? thanks for your help.
heres the code. i place<------------------------------------------------------------------------THE PROBLEM IS HERE so that you can easily find it.
Code :
import java.util.Scanner;
public class Blackjack {
public static void main(String[] args) {int money; // Amount of money the user has.
int bet; // Amount user bets on a game.
boolean userWins; // Did the user win the game?
System.out.println("Welcome to the game of blackjack.");
System.out.println();
money = 100; // User starts with $100.
Scanner reader = new Scanner(System.in);
while (true) {
System.out.println("You have " + money + " dollars.");
do {
System.out.println("How many dollars do you want to bet? (Enter 0 to end.)");
System.out.println("? ");
bet = reader.nextInt();
if (bet < 0 || bet > money)
System.out.println("Your answer must be between 0 and " + money + '.');
} while (bet < 0 || bet > money);
if (bet == 0)
break;
userWins = playBlackjack();
if (userWins)
money = money + bet;
else
money = money - bet;
System.out.println();
if (money == 0) {
System.out.println("Looks like you've run out of money!");
break;
}
}
System.out.println();
System.out.println("You leave with $" + money + '.');
}
static boolean playBlackjack() {
Deck deck; // A deck of cards. A new deck for each game.
BlackjackHand dealerHand; // The dealer's hand.
BlackjackHand userHand; // The user's hand.
deck = new Deck();
dealerHand = new BlackjackHand();
userHand = new BlackjackHand();
// Shuffle the deck, then deal two cards to each player.
deck.shuffle();
dealerHand.addCard( deck.dealCard() );
dealerHand.addCard( deck.dealCard() );
userHand.addCard( deck.dealCard() );
userHand.addCard( deck.dealCard() );
System.out.println();
System.out.println();
// Check if one of the players has Blackjack (two cards totaling to 21).
// The player with Blackjack wins the game. Dealer wins ties.
if (dealerHand.getBlackjackValue() == 21) {
System.out.println("Dealer has the " + dealerHand.getCard(0)
+ " and the " + dealerHand.getCard(1) + ".");
System.out.println("User has the " + userHand.getCard(0)
+ " and the " + userHand.getCard(1) + ".");
System.out.println();
System.out.println("Dealer has Blackjack. Dealer wins.");
return false;
}
if (userHand.getBlackjackValue() == 21) {
System.out.println("Dealer has the " + dealerHand.getCard(0)
+ " and the " + dealerHand.getCard(1) + ".");
System.out.println("User has the " + userHand.getCard(0)
+ " and the " + userHand.getCard(1) + ".");
System.out.println();
System.out.println("You have Blackjack. You win.");
return true;
}
while (true) {
// Display user's cards, and let user decide to Hit or Stand.
System.out.println();
System.out.println();
System.out.println("Your cards are:");
for ( int i = 0; i < userHand.getCardCount(); i++ )
System.out.println(" " + userHand.getCard(i));
System.out.println("Your total is " + userHand.getBlackjackValue());
System.out.println();
System.out.println("Dealer is showing the " + dealerHand.getCard(0));
System.out.println();
System.out.println("Hit (H) or Stand (S)? ");
char userAction;
do { // User's response, not 'H' or 'S'.
userAction = Character.toUpperCase(reader.getChar()); // <--------------------------------------------------------THE PROBLEM IS HERE
if (userAction != 'H' && userAction != 'S')
System.out.println("Please respond H or S: ");
} while (userAction != 'H' && userAction != 'S');
// If the user Hits, the user gets a card. If the user Stands, the loop ends (and it's the dealer's turn to draw cards).
if(userAction == 'S') {
// Loop ends; user is done taking cards.
break;
}
else { // userAction is 'H'. Give the user a card.
// If the user goes over 21, the user loses.
Card newCard = deck.dealCard();
userHand.addCard(newCard);
System.out.println();
System.out.println("User hits.");
System.out.println("Your card is the " + newCard);
System.out.println("Your total is now " + userHand.getBlackjackValue());
if (userHand.getBlackjackValue() > 21) {
System.out.println();
System.out.println("You busted by going over 21. You lose.");
System.out.println("Dealer's other card was the "
+ dealerHand.getCard(1));
return false;
}
}
}
// Now, it's the dealer's chance to draw. Dealer draws cards until the dealer's total is > 16. If dealer goes over 21, the dealer loses.
System.out.println();
System.out.println("User stands.");
System.out.println("Dealer's cards are");
System.out.println(" " + dealerHand.getCard(0));
System.out.println(" " + dealerHand.getCard(1));
while (dealerHand.getBlackjackValue() <= 16) {
Card newCard = deck.dealCard();
System.out.println("Dealer hits and gets the " + newCard);
dealerHand.addCard(newCard);
if (dealerHand.getBlackjackValue() > 21) {
System.out.println();
System.out.println("Dealer busted by going over 21. You win.");
return true;
}
}
System.out.println("Dealer's total is " + dealerHand.getBlackjackValue());
// If we get to this point, both players have 21 or less. Values are compared.
System.out.println();
if (dealerHand.getBlackjackValue() == userHand.getBlackjackValue()) {
System.out.println("Dealer wins on a tie. You lose.");
return false;
}
else if (dealerHand.getBlackjackValue() > userHand.getBlackjackValue()) {
System.out.println("Dealer wins, " + dealerHand.getBlackjackValue()
+ " points to " + userHand.getBlackjackValue() + ".");
return false;
}
else {
System.out.println("You win, " + userHand.getBlackjackValue()
+ " points to " + dealerHand.getBlackjackValue() + ".");
return true;
}
}
}
Re: A method not being resolved?
Quote:
It says that 'reader' cannot be resolved. What does that mean and what can I do to fix it?
The compiler message means that you have used a symbol (variable/method/class etc) and the compiler has no clue what it refers to.
The first thing to check is typos: reader must be spelt the same way when you use it as when you declare it.
With methods you must also supply the right number and type of arguments (if a method takes a String argument and you call it with an int or with no argument at all, the compiler will complain that the method you are using is unknown).
A third source of error involves scope. When a variable is declared it is always declared in some block or other. By block I mean from a { to the matching }. The variable is said to be "in scope" from the line where it is declared until the end of the block within which it is declared. This is important because you can use the variable while it is in scope, but not elsewhere. If you try and use a variable after the block in which it was declared has finished - that block can be a class, a method, or a block associated with a loop etc - then the compiler will complain that the variable is unknown.
Re: A method not being resolved?
Ask yourself: "I defined reader inside main and trying to use it inside playBlackjack() without passing as a parameter. Why in the world the compiler won't complain.".
You should modify your playBlackjack() and declare it to accept a formal parameter playBlackjack(Scanner reader). The when calling playBlackjack() inside method main pass the reader as a parameter i.e. change userWins = playBlackjack(); to userWins = playBlackjack(reader);