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.

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?

September 27th, 2012, 09:06 PM

curmudgeon

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

Quote:

Originally Posted by JSingh

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.

Code :

String myString = "";

September 27th, 2012, 09:10 PM

JSingh

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

Quote:

Originally Posted by curmudgeon

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.