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 5 of 5

Thread: Blackjack programming error (programmer is new to programming)

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Blackjack programming error (programmer is new to programming)

    hi, im new to java console programming and i needed a solution to what is likely very simple problem.

    every time i try running the program, it gives me an error saying
    "the variables strCard1 and strCard2 may be accessed here before having been assigned definite values".
    i'm trying to display the numbers the integers intCard1 and intCard2 generate with a card name (eg. Ace(1), King(10)) using the string values strCard1 and strCard2.
    it's the last line before the comments at the end causing the error.

    (using ReadyToProgramJava and Holt's output console)

     
    // The "Blackjack" class.
    import java.awt.*;
    import hsa.Console;
    import java.text.*;
    import java.util.Random;
     
    public class Blackjack
    {
        static Console c;           // The output console
     
        public static void main (String[] args)
        {
            c = new Console ();
     
            Random randomGenerator = new Random ();
     
            int intCard1, intCard2, intCardTotal, intFace1, intFace2;
            String strCard1, strCard2;
     
            //determine 1st card
            intCard1 = randomGenerator.nextInt (10) + 1; //Generate random number between 1 to 11.
            switch (intCard1)
     
                {
                    case 1:
                        strCard1 = ("Ace (1)");
                        break;
                    case 2:
                        strCard1 = ("Two(2)");
                        ;
                        break;
                    case 3:
                        strCard1 = ("Three(3)");
                        break;
                    case 4:
                        strCard1 = ("Four(4)");
                        break;
                    case 5:
                        strCard1 = ("Five(5)");
                        break;
                    case 6:
                        strCard1 = ("Six(6)");
                        break;
                    case 7:
                        strCard1 = ("Seven(7)");
                        break;
                    case 8:
                        strCard1 = ("Eight(8)");
                        break;
                    case 9:
                        strCard1 = ("Nine(9)");
                        break;
                    case 10:
                        intFace1 = randomGenerator.nextInt (2) + 1; //Generate random number between 1 to 3.
     
                        switch (intFace1)
                        {
                            case 1:
                                strCard1 = ("Jack(10)");
                                break;
                            case 2:
                                strCard1 = ("Queen(10)");
                                break;
                            case 3:
                                strCard1 = ("King(10)");
                                break;
                        }
                        break;
                    case 11:
                        strCard1 = ("Ace(11)");
                        break;
                }
     
     
            //determine 2nd card
            intCard2 = randomGenerator.nextInt (10) + 1; //Generate random number between 1 to 11.
            switch (intCard2)
     
                {
                    case 1:
                        strCard2 = ("Ace (1)");
                        break;
                    case 2:
                        strCard2 = ("Two(2)");
                        ;
                        break;
                    case 3:
                        strCard2 = ("Three(3)");
                        break;
                    case 4:
                        strCard2 = ("Four(4)");
                        break;
                    case 5:
                        strCard2 = ("Five(5)");
                        break;
                    case 6:
                        strCard2 = ("Six(6)");
                        break;
                    case 7:
                        strCard2 = ("Seven(7)");
                        break;
                    case 8:
                        strCard2 = ("Eight(8)");
                        break;
                    case 9:
                        strCard2 = ("Nine(9)");
                        break;
                    case 10:
                        intFace2 = randomGenerator.nextInt (2) + 1; //Generate random number between 1 to 3.
     
                        switch (intFace2)
                        {
                            case 1:
                                strCard2 = ("Jack(10)");
                                break;
                            case 2:
                                strCard2 = ("Queen(10)");
                                break;
                            case 3:
                                strCard2 = ("King(10)");
                                break;
                        }
                        break;
                    case 11:
                        strCard2 = ("Ace(11)");
                        break;
                }
     
            c.println ("You were dealt a(n) " + strCard1 + " and a(n) " + strCard2);
     
            // Place your program here.  'c' is the output console
        } // main method
    } // Blackjack class
    Last edited by JSingh; September 27th, 2012 at 09:58 PM.


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Blackjack programming error (programmer is new to programming)

    Programming is an exercise in exactness, and so for us to help you best, we need to know precisely what is wrong. You will not want to post "...or something..." but rather post the actual full error message and indicate which line is causing it. Why make us guess?

  3. #3
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Blackjack programming error (programmer is new to programming)

    Quote Originally Posted by JSingh View Post
    every time i try running the program, it gives me an error saying
    "the variables strCard1 and strCard2 may be accessed here before having been assigned definite values".
    You're assigning values to these variables in a switch / case block, and since the compiler doesn't know in advance which case will occur, or for that matter if *any* legitimate case will occur, it doesn't like you using the variables without first giving them some value, *any* value.

    So a solution is to give them an initial default value when you declare your variables. For Strings I often use the empty String "" for this.

    String myString = "";

  4. The Following User Says Thank You to curmudgeon For This Useful Post:

    JSingh (September 27th, 2012)

  5. #4
    Junior Member
    Join Date
    Sep 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Blackjack programming error (programmer is new to programming)

    Quote Originally Posted by curmudgeon View Post
    You're assigning values to these variables in a switch / case block, and since the compiler doesn't know in advance which case will occur, or for that matter if *any* legitimate case will occur, it doesn't like you using the variables without first giving them some value, *any* value.

    So a solution is to give them an initial default value when you declare your variables. For Strings I often use the empty String "" for this.

    String myString = "";
    thanks. it worked perfectly. appreciate it.

  6. #5
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Blackjack programming error (programmer is new to programming)

    You're welcome! Thanks for updating your question.

Similar Threads

  1. android programming vs game programming using java
    By vgoel38 in forum Android Development
    Replies: 4
    Last Post: September 8th, 2012, 06:48 PM
  2. How many programming languages a programmer could master in Real life.
    By voltaire in forum Other Programming Languages
    Replies: 7
    Last Post: July 26th, 2012, 10:35 AM
  3. Replies: 0
    Last Post: December 12th, 2011, 03:17 PM
  4. Serial Programming Logic Error???
    By bczm8703 in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: September 27th, 2011, 04:28 AM
  5. java programming error
    By priya_501 in forum Member Introductions
    Replies: 2
    Last Post: May 3rd, 2011, 12:15 PM

Tags for this Thread