Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: A method not being resolved?

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default 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.

    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;
          }
     
       }  
     
     
    }


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: A method not being resolved?

    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.

  3. The Following User Says Thank You to pbrockway2 For This Useful Post:

    inflames098 (October 23rd, 2012)

  4. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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);

Similar Threads

  1. StdDraw Cannot be Resolved??
    By jbarcus81 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 28th, 2014, 05:47 PM
  2. mp3 file cant be resolved
    By aNGaJe in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 2nd, 2012, 07:20 AM
  3. Highlighted words Not resolved
    By Rajiv in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 4th, 2011, 07:28 AM
  4. cannot be resolved to a type
    By Teraphim in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 16th, 2010, 10:42 AM
  5. Error msg : 'pageContext' cannot be resolved in tag file....
    By saikrishna436 in forum JavaServer Pages: JSP & JSTL
    Replies: 5
    Last Post: September 8th, 2009, 07:58 AM